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 StampToString(long stamp) {
    /*/*from   ww  w .j a  v  a2  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 = STAMP_TO_STRING;
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    sdf.setTimeZone(TimeZone.getTimeZone(TIME_ZONE_PRC));

    return sdf.format(date);

}

From source file:Main.java

public static String getDateCurrentTimeZone() {
    StringBuilder sb = new StringBuilder();
    try {// w w w.j  ava2s  .  com
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT,
                Locale.getDefault());
        Date currentTimeZone = calendar.getTime();
        sb.append(dateFormat.format(currentTimeZone));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Returns the Date from the long milliseconds
 * //from  ww  w  .ja  v  a2  s  . co m
 * @param date
 *            in millis
 * @return {@link String}
 */
public static String GetDateFromLong(long date) {
    Date currentDate = new Date(date);
    DateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
    // formatted value of current Date
    // System.out.println("Milliseconds to Date: " +
    // formatter.format(currentDate));
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(date);
    // System.out.println("Milliseconds to Date using Calendar:"
    // + formatter.format(cal.getTime()));
    return currentDate.toString();

}

From source file:Main.java

public static boolean isSameDay(long lhs, long rhs) {
    Calendar lhsCal = Calendar.getInstance();
    Calendar rhsCal = Calendar.getInstance();
    lhsCal.setTimeInMillis(lhs);
    rhsCal.setTimeInMillis(rhs);//  ww  w  . j  av  a2 s .  c  om
    return (lhsCal.get(Calendar.YEAR) == rhsCal.get(Calendar.YEAR))
            && (lhsCal.get(Calendar.DAY_OF_YEAR) == rhsCal.get(Calendar.DAY_OF_YEAR));
}

From source file:Main.java

public static int getStudyDay(Context context) {
    String startStr = getSP(context, SP_LOGIN).getString(SP_KEY_LOGIN_STUDY_STARTTIME, "");
    if (!startStr.equals("")) {
        long start = Long.parseLong(startStr);
        long current = Calendar.getInstance().getTimeInMillis();

        Calendar s = Calendar.getInstance();
        s.setTimeInMillis(start);
        s.set(Calendar.HOUR_OF_DAY, 3);
        s.set(Calendar.MINUTE, 0);
        s.set(Calendar.SECOND, 0);
        s.set(Calendar.MILLISECOND, 0);

        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(current);/*from  w  ww  . j  a  v a  2 s  .com*/
        //         c.set(Calendar.HOUR_OF_DAY, 12);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);

        start = s.getTimeInMillis();
        current = c.getTimeInMillis();

        return (int) ((current - start) / (24 * 60 * 60 * 1000));
    } else {
        return -1;
    }
}

From source file:Main.java

/**
 * if two date is the same day, for example ,1999-11-21 00:00:00 and  1999-11-21 23:15:40 are in same day,
 * but 1999-11-21 00:00:00 and 2010-11-21 00:00:00 are not
 *
 * @param ltime/*  w w  w .  j a va2 s  . c om*/
 * @param rtime
 * @return
 */
public static boolean sameDay(long ltime, long rtime) {
    Calendar l = Calendar.getInstance();
    Calendar r = Calendar.getInstance();
    l.setTimeInMillis(ltime);
    r.setTimeInMillis(rtime);

    return l.get(Calendar.YEAR) == r.get(Calendar.YEAR) && l.get(Calendar.MONTH) == r.get(Calendar.MONTH)
            && l.get(Calendar.DAY_OF_MONTH) == r.get(Calendar.DAY_OF_MONTH);
}

From source file:Main.java

/**
 * Get Calendar from Date//from  w  w w.j  a va2 s .c  o  m
 * @param date Date
 * @return Calendar generated
 */
public static Calendar generateCalendar(Date date) {
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(date.getTime());
    return c;
}

From source file:Main.java

/**
 * Get Calendar from Date/* w ww  . j  a  v  a  2 s.  co m*/
 * @param cal Calendar
 * @return Calendar generated
 */
public static Calendar generateCalendar(Calendar cal) {
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(cal.getTimeInMillis());
    return c;
}

From source file:Main.java

public static String ArrivalTime(long arrivalMilliseconds, String timeformat) {

    DateFormat formatter;/*from  w ww.j  a v a 2  s  .  co m*/
    Date convertedDate = null;

    Calendar calendarToday = Calendar.getInstance();
    calendarToday.setTimeInMillis(arrivalMilliseconds);

    String Time = String
            .valueOf(calendarToday.get(Calendar.HOUR_OF_DAY) + ":" + calendarToday.get(Calendar.MINUTE));
    formatter = new SimpleDateFormat("HH:mm");
    try {
        convertedDate = (Date) formatter.parse(Time);

    } catch (ParseException e) {
        //e.printStackTrace();
    }

    if (timeformat.equals("24"))
        return formatter.format(convertedDate);
    else
        return ampmChanger(formatter.format(convertedDate));

}

From source file:Main.java

public static boolean isToday(long dateUtc) {
    final long currentTime = System.currentTimeMillis();
    final Calendar calendarBefore = Calendar.getInstance();
    calendarBefore.setTimeInMillis(currentTime);
    calendarBefore.set(Calendar.HOUR_OF_DAY, 0);
    calendarBefore.set(Calendar.MINUTE, 0);
    calendarBefore.set(Calendar.SECOND, 0);
    calendarBefore.set(Calendar.MILLISECOND, 0);

    ///*  ww  w.j  a v  a 2  s. co  m*/
    final Calendar calendarAfter = Calendar.getInstance();
    calendarAfter.setTimeInMillis(currentTime);
    calendarAfter.set(Calendar.HOUR_OF_DAY, 23);
    calendarAfter.set(Calendar.MINUTE, 59);
    calendarAfter.set(Calendar.SECOND, 59);
    calendarAfter.set(Calendar.MILLISECOND, 250);
    if ((dateUtc >= calendarBefore.getTimeInMillis()) && (dateUtc <= calendarAfter.getTimeInMillis())) {
        return true;
    }
    return false;
}