Example usage for java.text DateFormat format

List of usage examples for java.text DateFormat format

Introduction

In this page you can find the example usage for java.text DateFormat format.

Prototype

public final String format(Date date) 

Source Link

Document

Formats a Date into a date-time string.

Usage

From source file:TimeUtil.java

public static String windowFormat(long msecs) {
    Date tDate = new Date(msecs);
    long now = System.currentTimeMillis();
    long diff = now - msecs;
    // start with the last 24 hours
    long comp = MS_PER_DAY;
    // start comparing
    if (diff < comp) {
        // it's within the last 24 hours - just return the time
        DateFormat formatter = DateFormat.getTimeInstance(DateFormat.SHORT);
        return formatter.format(tDate);
    }//from   w  w  w  .j a v a  2 s .c o m
    // now expand to another day
    comp += MS_PER_DAY;
    if (diff < comp) {
        // it's within the last 48 hours - just return the time
        DateFormat formatter = DateFormat.getTimeInstance(DateFormat.SHORT);
        return "Yesterday " + formatter.format(tDate);
    }
    // now up it to a week
    comp += 5 * MS_PER_DAY;
    if (diff < comp) {
        // return the day of the week and the time
        // get time to be formatted into a calendar
        GregorianCalendar tCal = new GregorianCalendar();
        tCal.setTime(tDate);
        SimpleDateFormat formatter = new SimpleDateFormat("EEEE");
        return formatter.format(tDate);
    }
    // just return full date
    DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
    return formatter.format(tDate);
}

From source file:Main.java

public static void writeCSV(String phoneNo, String Message) {
    File root = Environment.getExternalStorageDirectory();
    File csvFile = new File(root, "/adspammer/log.csv");
    FileWriter writer = null;/*from  ww  w . j  ava  2 s  .  c om*/
    try {
        writer = new FileWriter(csvFile, true);

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd @ HH:mm:ss");
        Date date = new Date();
        System.out.println(dateFormat.format(date));

        writer.append(dateFormat.format(date).toString());
        writer.append(',');
        writer.append(phoneNo);
        writer.append(',');
        writer.append(Message);
        writer.append('\n');

        writer.flush();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:com.collabnet.ccf.core.utils.DateUtil.java

private static String format(Date date, DateFormat dateFormat) {
    return dateFormat.format(date);
}

From source file:Main.java

public static String getCurrentDateTimeinMillis() {
    /*//from  w w w  .java2 s. co m
    long millis = System.currentTimeMillis()%1000;
    String millisStr;
    if(millis < 10){
    millisStr = "00" + String.valueOf(millis);
    }else if(millis < 100){
    millisStr = "0" + String.valueOf(millis);
    }else{
    millisStr = String.valueOf(millis);
    }
    String currentDateTimeinMillis = Helper.getCurrentDateTime()
        + "." + millisStr;*/

    DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HHmmss.SSS");
    Calendar cal = Calendar.getInstance();
    String currentDateTimeinMillis = dateFormat.format(cal.getTime());
    return currentDateTimeinMillis;
}

From source file:Main.java

/**
 * Method to change update date in xml files - used for reloadable issue and auto change on start up.
 * //from  w  w w.j  a v a2  s  . c o m
 * @param target document
 * @return updated document
 * @throws ParseException
 */
private static Document changeDate(Document target) throws ParseException {
    NodeList parents = target.getElementsByTagName("g:options");
    Element parent = (Element) parents.item(0); //g:options - only 1 element
    DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH);
    Date newT = new Date();
    String time = format.format(newT);
    parent.setAttribute("time", time);
    return target;
}

From source file:Main.java

public static String addDay(int day) {
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd", Locale.getDefault());
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, day);//from   w  ww  . j  a v a 2  s.c om
    return dateFormat.format(cal.getTime());

}

From source file:org.devdom.commons.util.Utils.java

/**
 * //from  w  ww.j ava2s .  c o  m
 * Extraer fecha formateada, indicando cual es el formato 
 * 
 * @param date
 * @param format
 * @return 
 */
public static String getDateFormatted(Date date, String format) {
    DateFormat formatter = new SimpleDateFormat(format);
    return formatter.format(date);
}

From source file:foss.filemanager.core.Utils.java

public static File arrayByteToFile(byte[] fileBArray) throws FileNotFoundException, IOException {
    DateFormat df = new SimpleDateFormat("ddMMyyyy-hhmmss-S");
    String name = df.format(new Date());
    String tmp = System.getProperty("java.io.tmpdir");
    File file = new File(tmp, name);
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(fileBArray);/*from   w w  w  .j a va  2  s.  co  m*/
    fos.close();
    return file;
}

From source file:Main.java

public static String DateTimeToString(Date d) {
    DateFormat formatter;

    formatter = new SimpleDateFormat("dd-MM-yyyy-HH-mm");
    formatter.setTimeZone(TimeZone.getTimeZone("EST"));

    String s = new StringBuilder(formatter.format(d)).toString();
    return s;// w w  w  . j a  v a2 s  .  c  om
}

From source file:Main.java

public static String getCurrentUtcTime() {
    Date l_datetime = new Date();
    DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
    TimeZone l_timezone = TimeZone.getDefault();
    formatter.setTimeZone(l_timezone);/*from www  .j av  a2s  . c  o  m*/
    String l_utc_date = formatter.format(l_datetime);
    return l_utc_date;
}