Example usage for java.util Calendar SECOND

List of usage examples for java.util Calendar SECOND

Introduction

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

Prototype

int SECOND

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

Click Source Link

Document

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

Usage

From source file:Main.java

/**
 * Get the SunRise for the given date/*from   ww w. j a v  a2  s  . c o  m*/
 * @param date - input date
 * @return - Date with time specifying the Sun Rise
 */
public static Date getSunRise(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.HOUR_OF_DAY, 06);
    cal.set(Calendar.MINUTE, 00);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    Date ret = cal.getTime();
    return ret;
}

From source file:Main.java

public static Calendar getCalendarForTime(int repeatMode, int dayOfWeek, int hour, int minute, int second) {
    Calendar calendarNow = Calendar.getInstance();
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.SECOND, second);
    calendar.set(Calendar.MILLISECOND, 0);
    if (repeatMode == 2) {
        int dayOfWeekAndroid = 0; // 7 stands for sunday for interface, but for android, sunday stands for 1.
        dayOfWeekAndroid = dayOfWeek % 7 + 1;
        calendar.set(Calendar.DAY_OF_WEEK, dayOfWeekAndroid);
    }/* w  w  w.j av a  2  s  . c om*/

    // make sure the desire alarm time is in future.
    int tryCount = 0;
    int tryCountMax = 62;
    while (calendar.getTimeInMillis() < calendarNow.getTimeInMillis() && tryCount < tryCountMax) {
        if (repeatMode == 1) {
            calendar.add(Calendar.DAY_OF_YEAR, 1);
        } else if (repeatMode == 2) {
            calendar.add(Calendar.DAY_OF_YEAR, 7);
        }
        tryCount++;
    }
    Log.v("cpeng", "getCalendearForTime target info: " + calendar.toString());
    return calendar;
}

From source file:Main.java

public static Date lastTimeOfMonth(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);/* w ww  .j a  va 2s . co  m*/
    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    cal.set(Calendar.HOUR_OF_DAY, cal.getActualMaximum(Calendar.HOUR_OF_DAY));
    cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE));
    cal.set(Calendar.SECOND, cal.getActualMaximum(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, cal.getActualMaximum(Calendar.MILLISECOND));
    return cal.getTime();
}

From source file:Util.java

public static String formatAsMySQLDatetime(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);/*from ww  w  .  ja v a  2 s  .c om*/

    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH) + 1;
    int day = calendar.get(Calendar.DATE);

    int hours = calendar.get(Calendar.HOUR_OF_DAY);
    int minutes = calendar.get(Calendar.MINUTE);
    int seconds = calendar.get(Calendar.SECOND);

    return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
}

From source file:Main.java

public static Date firstTimeOfMonth(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);//from   w w w. j av  a2s. c  om
    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
    cal.set(Calendar.HOUR_OF_DAY, cal.getActualMinimum(Calendar.HOUR_OF_DAY));
    cal.set(Calendar.MINUTE, cal.getActualMinimum(Calendar.MINUTE));
    cal.set(Calendar.SECOND, cal.getActualMinimum(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, cal.getActualMinimum(Calendar.MILLISECOND));
    return cal.getTime();
}

From source file:Main.java

/**
 * Remove the time of a date value//from  w  w  w.j  av  a2 s.co m
 *
 * @param date Date to remove the time part
 * @return A date with its time set to 00:00:00
 */
public static Date removeTime(Date date) {
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(date);
    gc.set(Calendar.HOUR_OF_DAY, 0);
    gc.set(Calendar.MINUTE, 0);
    gc.set(Calendar.SECOND, 0);
    gc.set(Calendar.MILLISECOND, 0);
    return gc.getTime();
}

From source file:Main.java

public static Calendar transformStringTimeToCalendar(String time) {
    Calendar calendar = Calendar.getInstance();
    String[] split = time.split(":");
    if (split.length > 0) {
        calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(split[0]));
    }/*from   ww w  .  j a  va2 s . c  o m*/
    if (split.length > 1) {
        calendar.set(Calendar.MINUTE, Integer.parseInt(split[1]));
    }
    if (split.length > 2) {
        calendar.set(Calendar.SECOND, Integer.parseInt(split[2]));
    }
    return calendar;
}

From source file:Main.java

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

    date.set(Calendar.YEAR, year);
    date.set(Calendar.MONTH, month);
    date.set(Calendar.DAY_OF_MONTH, day);
    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 getStartOfDayMs(int year, int month, int day) {
    final Calendar date = Calendar.getInstance();

    date.set(Calendar.YEAR, year);
    date.set(Calendar.MONTH, month);
    date.set(Calendar.DAY_OF_MONTH, day);
    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:Util.java

/**
 * Utility for setting the time on a date.
 * @param date// w w  w .  j ava 2s .  c o  m
 * @param h
 * @param m
 * @param s
 * @return the date with the time set
 */
public static Date setTime(Date date, int h, int m, int s) {
    if (date == null)
        return null;
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.HOUR, h);
    calendar.set(Calendar.MINUTE, m);
    calendar.set(Calendar.SECOND, s);
    return calendar.getTime();
}