Example usage for java.util Calendar setTimeInMillis

List of usage examples for java.util Calendar setTimeInMillis

Introduction

In this page you can find the example usage for java.util Calendar setTimeInMillis.

Prototype

public void setTimeInMillis(long millis) 

Source Link

Document

Sets this Calendar's current time from the given long value.

Usage

From source file:Main.java

public static String StampToStringMMddHHmm(long stamp) {

    Date date = null;//from w w  w.  j a v  a  2s . co m
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(stamp);
    date = calendar.getTime();

    //       String format = "yyyy-MM-dd HH:mm:ss.SSSZ"; 
    String format = "MM-dd HH:mm";
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    //sdf.setTimeZone(TimeZone.getTimeZone("UTC"));   
    sdf.setTimeZone(TimeZone.getTimeZone("PRC"));

    return sdf.format(date);

}

From source file:Main.java

/**
 * Return date in specified format./*from ww w.j a  v a  2 s. com*/
 * @param milliSeconds Date in milliseconds
 * @param dateFormat Date format 
 * @return String representing date in specified format
 */
public static String getDate(long milliSeconds) {
    String dateFormat = "dd/MM/yyyy hh:mm:ss"; //.SSS";
    // Create a DateFormatter object for displaying date in specified format.
    SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);

    // Create a calendar object that will convert the date and time value in milliseconds to date. 
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(milliSeconds);

    Log.d("EarthquakeMonitor",
            "Helpers.getDate result => " + milliSeconds + " to " + formatter.format(calendar.getTime()));

    return formatter.format(calendar.getTime());
}

From source file:Main.java

public static String StampToyyyyMMddHHmmss(long stamp) {

    Date date = null;/*from w  w w . j  a v  a 2 s  . co  m*/
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(stamp);
    date = calendar.getTime();

    String format = "yyyy-MM-dd HH:mm:ss.SSSZ";
    //       String format = "yyyy-MM-dd HH:mm:ss"; 
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    //sdf.setTimeZone(TimeZone.getTimeZone("UTC"));   
    sdf.setTimeZone(TimeZone.getTimeZone("PRC"));

    String time = sdf.format(date);
    return time.substring(0, "yyyy-MM-dd HH:mm:ss".length());

}

From source file:Main.java

public static String getTime() {
    StringBuffer sb = new StringBuffer();
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(System.currentTimeMillis());
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minutes = c.get(Calendar.MINUTE);
    if (hour < 10) {
        sb.append(0);/* ww w. j  ava 2 s.co  m*/
        sb.append(hour);
    } else {
        sb.append(hour);
    }
    sb.append(':');
    if (minutes < 10) {
        sb.append(0);
        sb.append(minutes);
    } else {
        sb.append(minutes);
    }
    return sb.toString();

}

From source file:Main.java

public static int getDatePart(long time, int part) {
    if (time <= 0)
        return 0;
    Calendar cal = Calendar.getInstance(Locale.getDefault());
    cal.setTimeInMillis(time);
    return cal.get(part);
}

From source file:Main.java

public static Date firstTimeOfDate(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(date.getTime());

    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    Date dateBegin = new Date();
    dateBegin.setTime(calendar.getTimeInMillis());
    return dateBegin;
}

From source file:Main.java

public static Calendar localize(Calendar calendar) {
    if (calendar == null) {
        return calendar;
    }//from  www  .j a v  a  2  s . co m
    final Calendar localCalendar = Calendar.getInstance();
    localCalendar.setTimeInMillis(calendar.getTimeInMillis());
    return localCalendar;
}

From source file:Main.java

/**
 * Converts input time from Java to DOS format
 * @param time//  w  w  w.  j  a  va  2s .  co m
 * @return time in DOS format 
 */
public static long javaToDosTime(long time) {

    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(time);

    int year = cal.get(Calendar.YEAR);
    if (year < 1980) {
        return (1 << 21) | (1 << 16);
    }
    return (year - 1980) << 25 | (cal.get(Calendar.MONTH) + 1) << 21 | cal.get(Calendar.DATE) << 16
            | cal.get(Calendar.HOUR_OF_DAY) << 11 | cal.get(Calendar.MINUTE) << 5
            | cal.get(Calendar.SECOND) >> 1;
}

From source file:Main.java

public static String getTimeString(long millitm) {
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cal.setTimeInMillis(millitm);
    return (setZeroPad(cal.get(Calendar.HOUR)) + ":" + setZeroPad(cal.get(Calendar.MINUTE)) + ":"
            + setZeroPad(cal.get(Calendar.SECOND)));
}

From source file:Main.java

public static String getMsgDate(long msg_time) {
    Calendar cl = Calendar.getInstance();
    cl.setTimeInMillis(msg_time); //here your time in miliseconds
    String date = "";

    String[] monthNames = { "January", "February", "March", "April", "May", "June", "July", "August",
            "September", "October", "November", "December" };

    SimpleDateFormat month_date = new SimpleDateFormat("MMM");
    String month_name = monthNames[cl.get(Calendar.MONTH)];

    int day = cl.get(Calendar.DAY_OF_MONTH);
    int year = cl.get(Calendar.YEAR);

    int current_year = Calendar.getInstance().get(Calendar.YEAR);
    if (current_year == year) {
        date = month_name + " " + day;
    } else {/*  w  ww .  jav a  2s  .  c  om*/
        date = month_name + " " + day + ", " + year;
    }

    return date;
}