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[] getCalendarShowTime(String paramString) {
    try {/*from  w  w  w.  j a va  2  s.  c  om*/
        long l = Long.valueOf(paramString);
        Calendar localCalendar = Calendar.getInstance();
        localCalendar.setTimeInMillis(1000L * l);
        return getCalendarShowTime(localCalendar.getTimeInMillis());
    } catch (NumberFormatException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Return date in specified format.//from w  ww . ja  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) {
    // Create a DateFormatter object for displaying date in specified format.
    DateFormat 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);
    return formatter.format(calendar.getTime());
}

From source file:Main.java

public static int getItemsCounts(int periodType, long date) {
    switch (periodType) {
    case TYPE_DAY:
        return 24;

    case TYPE_WEEK:
        return 7;

    case TYPE_MONTH: {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(date);
        return c.getMaximum(Calendar.DAY_OF_MONTH);
    }/* w w  w  . j  av  a  2  s  . c o m*/

    case TYPE_YEAR: {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(date);
        return c.getMaximum(Calendar.MONTH) + 1;
    }
    }

    return 0;
}

From source file:Main.java

public static String formatDateDiff(long date) {
    Calendar c = new GregorianCalendar();
    c.setTimeInMillis(date);
    Calendar now = new GregorianCalendar();
    return formatDateDiff(now, c);
}

From source file:Main.java

public static Date firstTimeOfMonth(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);
    calendar.set(Calendar.DAY_OF_MONTH, 1);

    Date dateBegin = new Date();
    dateBegin.setTime(calendar.getTimeInMillis());
    return dateBegin;
}

From source file:Main.java

public static String getDate(final long time) {
    final Calendar cal = Calendar.getInstance(Locale.ENGLISH);
    cal.setTimeInMillis(time);
    return DateFormat.format("dd-MM-yyyy", cal).toString();
}

From source file:Main.java

public static String StampToString(long stamp) {
    /*//from   ww w. ja va  2  s .c  o  m
     String[] formats = new String[] {  
     "yyyy-MM-dd",   
     "yyyy-MM-dd HH:mm",  
     "yyyy-MM-dd HH:mmZ",   
     "yyyy-MM-dd HH:mm:ss.SSSZ",
     "yyyy-MM-dd'T'HH:mm:ss.SSSZ",
     }; 
     */
    Date date = null;
    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.SSSZ";
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    //sdf.setTimeZone(TimeZone.getTimeZone("UTC"));   
    sdf.setTimeZone(TimeZone.getTimeZone("PRC"));

    return sdf.format(date);

}

From source file:Main.java

public static long convertTime(long timestamp, String fromTimeZone, String toTimeZone) {
    Calendar fromCal = new GregorianCalendar(TimeZone.getTimeZone(fromTimeZone));
    fromCal.setTimeInMillis(timestamp);
    Calendar toCal = new GregorianCalendar(TimeZone.getTimeZone(toTimeZone));
    toCal.setTimeInMillis(fromCal.getTimeInMillis());
    return toCal.getTimeInMillis();
}

From source file:Main.java

/**
 * Determines if the current time is between fromMillis and toMillis
 * Does so by calculating an inverse time range based on the minutes
 *
 * @param currentMillis current time in milliseconds
 * @param fromMillis    start time in milliseconds
 * @param toMillis      end time in milliseconds
 * @return whether or not the currentMillis is between fromMillis and toMillis
 *///from w w  w. j a v a 2 s .  c  om
static boolean isTimeInRange(long currentMillis, long fromMillis, long toMillis) {
    final Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(currentMillis);

    final int currentMinuteOfDay = calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE);
    calendar.setTimeInMillis(fromMillis);

    final int fromMinuteOfDay = calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE);
    calendar.setTimeInMillis(toMillis);

    final int toMinuteOfDay = calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE);

    if (fromMinuteOfDay <= toMinuteOfDay) {
        return (currentMinuteOfDay >= fromMinuteOfDay && currentMinuteOfDay < toMinuteOfDay);
    } else {
        return (currentMinuteOfDay >= fromMinuteOfDay || currentMinuteOfDay < toMinuteOfDay);
    }
}

From source file:Main.java

public static long convertTZ(long timestamp, String fromTimeZone, String toTimeZone) {
    Calendar fromCal = new GregorianCalendar(TimeZone.getTimeZone(fromTimeZone));
    fromCal.setTimeInMillis(timestamp);
    Calendar toCal = new GregorianCalendar(TimeZone.getTimeZone(toTimeZone));
    toCal.setTimeInMillis(fromCal.getTimeInMillis());
    return toCal.getTimeInMillis();
}