Example usage for java.util Calendar MILLISECOND

List of usage examples for java.util Calendar MILLISECOND

Introduction

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

Prototype

int MILLISECOND

To view the source code for java.util Calendar MILLISECOND.

Click Source Link

Document

Field number for get and set indicating the millisecond within the second.

Usage

From source file:Main.java

public static long getEndOfMonthMs(int year, int month) {
    final Calendar date = Calendar.getInstance();

    date.set(Calendar.YEAR, year);
    date.set(Calendar.MONTH, month);
    date.set(Calendar.DAY_OF_MONTH, date.getActualMaximum(Calendar.DAY_OF_MONTH));
    date.set(Calendar.HOUR_OF_DAY, date.getActualMaximum(Calendar.HOUR_OF_DAY));
    date.set(Calendar.MINUTE, date.getActualMaximum(Calendar.MINUTE));
    date.set(Calendar.SECOND, date.getActualMaximum(Calendar.SECOND));
    date.set(Calendar.MILLISECOND, date.getActualMaximum(Calendar.MILLISECOND));
    return date.getTimeInMillis();
}

From source file:Main.java

public static long getStartOfMonthMs(int year, int month) {
    final Calendar date = Calendar.getInstance();

    date.set(Calendar.YEAR, year);
    date.set(Calendar.MONTH, month);
    date.set(Calendar.DAY_OF_MONTH, date.getActualMinimum(Calendar.DAY_OF_MONTH));
    date.set(Calendar.HOUR_OF_DAY, date.getActualMinimum(Calendar.HOUR_OF_DAY));
    date.set(Calendar.MINUTE, date.getActualMinimum(Calendar.MINUTE));
    date.set(Calendar.SECOND, date.getActualMinimum(Calendar.SECOND));
    date.set(Calendar.MILLISECOND, date.getActualMinimum(Calendar.MILLISECOND));
    return date.getTimeInMillis();
}

From source file:Main.java

public static Date getTime(int year, int month, int day) {
    Calendar date = Calendar.getInstance();
    date.set(year, month, day, 0, 0, 0);
    date.set(Calendar.MILLISECOND, 0);
    return date.getTime();
}

From source file:Main.java

public static Date getDateTimeFrom(int hour, int minute) {
    Calendar calendar = Calendar.getInstance();

    calendar.set(Calendar.DAY_OF_MONTH, 0);
    calendar.set(Calendar.MONTH, 0);
    calendar.set(Calendar.YEAR, 0);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    Date ret = calendar.getTime();
    return ret;/*from w w  w . j av a2  s  .  c om*/
}

From source file:Main.java

public static long getYearStart() {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_YEAR, 1);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);/*from  w  ww .j  a  va2 s .c o  m*/
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTimeInMillis();
}

From source file:Main.java

public static long getPeriodStart(int periodType, long date) {
    final Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(date);// ww w.  java2 s  . c o m

    switch (periodType) {
    case TYPE_DAY: {
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        break;
    }

    case TYPE_WEEK: {
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        final int currentDayOfWeek = (cal.get(Calendar.DAY_OF_WEEK) + 7 - cal.getFirstDayOfWeek()) % 7;
        cal.add(Calendar.DAY_OF_YEAR, -currentDayOfWeek);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        break;
    }

    case TYPE_MONTH: {
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        break;
    }

    case TYPE_YEAR: {
        cal.set(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        break;
    }
    }
    return cal.getTimeInMillis();
}

From source file:Main.java

public static boolean isCheckChangePermitted(Context context, Calendar date) {

    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    String limitActivePref = sharedPrefs.getString("active-date-key", "ALLOW_ALL");

    Calendar today = Calendar.getInstance();
    today.set(Calendar.HOUR_OF_DAY, 0);
    today.set(Calendar.MINUTE, 0);
    today.set(Calendar.SECOND, 0);
    today.set(Calendar.MILLISECOND, 0);

    Calendar yesterday;// w  w  w .  j  ava  2 s  . com

    switch (limitActivePref) {
    case "ALLOW_CURRENT":
        return (date.compareTo(today) == 0);
    case "ALLOW_CURRENT_AND_NEXT_DAY":
        yesterday = (Calendar) today.clone();
        yesterday.add(Calendar.DATE, -1);
        return (date.compareTo(yesterday) >= 0);
    case "ALLOW_CURRENT_AND_NEXT_DAY_AND_WEEKEND":
        yesterday = (Calendar) today.clone();
        yesterday.add(Calendar.DATE, today.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY ? -2 : -1);
        return (date.compareTo(yesterday) >= 0);
    case "ALLOW_ALL":
    default:
        return true;
    }
}

From source file:Main.java

public static LinkedHashMap<String, Integer> getDateMap(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);// w  w w .j  av  a2  s  .  c  o  m
    LinkedHashMap<String, Integer> datemap = new LinkedHashMap<String, Integer>();
    datemap.put("year", cal.get(Calendar.YEAR));
    datemap.put("month", cal.get(Calendar.MONTH));
    datemap.put("day", cal.get(Calendar.DAY_OF_MONTH));
    datemap.put("hour", cal.get(Calendar.HOUR_OF_DAY));
    datemap.put("minute", cal.get(Calendar.MINUTE));
    datemap.put("second", cal.get(Calendar.SECOND));
    datemap.put("millisecond", cal.get(Calendar.MILLISECOND));

    return datemap;

}

From source file:Main.java

public static void addCalendarMillisecond(Calendar cal, int milliseconds) {
    cal.add(Calendar.MILLISECOND, milliseconds);
}

From source file:Main.java

public static Date dayEnd(final Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);/*from w w  w .  j  ava  2s. co m*/
    c.set(Calendar.HOUR_OF_DAY, 23);
    c.set(Calendar.MINUTE, 59);
    c.set(Calendar.SECOND, 59);
    c.set(Calendar.MILLISECOND, 999);
    return c.getTime();
}