Example usage for java.util Calendar set

List of usage examples for java.util Calendar set

Introduction

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

Prototype

public void set(int field, int value) 

Source Link

Document

Sets the given calendar field to the given value.

Usage

From source file:Main.java

private static long getTimeInMillis() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 21);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    return calendar.getTimeInMillis();
}

From source file:Main.java

/**
 * Returns Date object of the next day of d.
 * @return//  w  w  w .java  2  s  .  c o  m
 */
public static Date getNextDay(Date d) {
    Calendar c = Calendar.getInstance();
    if (d != null)
        c.setTime(d);
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    c.add(Calendar.DAY_OF_MONTH, 1);
    return c.getTime();
}

From source file:Main.java

public static Long getToday() {
    Calendar today = Calendar.getInstance();
    today.setTimeZone(TimeZone.getDefault());

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

    // FOR DEBUG/*from www .j  a va  2 s  .  c  om*/
    //today.set(Calendar.DAY_OF_YEAR, today.get(Calendar.DAY_OF_YEAR) + 1);

    return today.getTimeInMillis();
}

From source file:Main.java

public static String getMonthName(int month) {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat month_date = new SimpleDateFormat("MMM");
    cal.set(Calendar.MONTH, month - 1);
    String month_name = month_date.format(cal.getTime());
    return month_name;
}

From source file:Main.java

public static void previousDateString(String dateString) throws ParseException {
    DateFormat inputDateFormat = new SimpleDateFormat("dd-MM-yyyy");
    DateFormat outputDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    Calendar now = Calendar.getInstance();
    Calendar calendar = Calendar.getInstance();
    Date myDate = inputDateFormat.parse(dateString);
    calendar.setTime(myDate);/* w ww .j  av a  2  s  . c  om*/
    calendar.set(Calendar.HOUR_OF_DAY, now.get(Calendar.HOUR_OF_DAY));
    calendar.set(Calendar.MINUTE, now.get(Calendar.MINUTE));

    String output = outputDateFormat.format(calendar.getTime());
    System.out.println(output);
}

From source file:Main.java

public static long getTimeStampStartOfDay(long time) {
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    cal.setTime(new Date(time));
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);//from  www.  ja v a 2 s . co  m
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTimeInMillis();
}

From source file:Main.java

public static int getGapDay(Date startDate, Date endDate) {
    Calendar fromCalendar = Calendar.getInstance();
    fromCalendar.setTime(startDate);//from w  w w  .j a  va2  s  .  c  o m
    fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
    fromCalendar.set(Calendar.MINUTE, 0);
    fromCalendar.set(Calendar.SECOND, 0);
    fromCalendar.set(Calendar.MILLISECOND, 0);

    Calendar toCalendar = Calendar.getInstance();
    toCalendar.setTime(endDate);
    toCalendar.set(Calendar.HOUR_OF_DAY, 0);
    toCalendar.set(Calendar.MINUTE, 0);
    toCalendar.set(Calendar.SECOND, 0);
    toCalendar.set(Calendar.MILLISECOND, 0);

    return (int) ((toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24));
}

From source file:Main.java

public static int getGapCount(Date startDate, Date endDate) {
    Calendar fromCalendar = Calendar.getInstance();
    fromCalendar.setTime(startDate);//w  w w.jav a 2  s  . c  o m
    fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
    fromCalendar.set(Calendar.MINUTE, 0);
    fromCalendar.set(Calendar.SECOND, 0);
    fromCalendar.set(Calendar.MILLISECOND, 0);

    Calendar toCalendar = Calendar.getInstance();
    toCalendar.setTime(endDate);
    toCalendar.set(Calendar.HOUR_OF_DAY, 0);
    toCalendar.set(Calendar.MINUTE, 0);
    toCalendar.set(Calendar.SECOND, 0);
    toCalendar.set(Calendar.MILLISECOND, 0);

    return (int) ((toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24));
}

From source file:Main.java

public static List<String> getWeekDate() {
    List<String> list = new ArrayList<String>();
    Calendar calendar = Calendar.getInstance(Locale.CHINA);
    calendar.setTime(new Date());
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    list.add(calendar.get(Calendar.MONTH) + 1 + "." + calendar.get(Calendar.DATE));
    for (int i = 0; i < 6; i++) {
        calendar.add(Calendar.DATE, 1);
        String str = calendar.get(Calendar.MONTH) + 1 + "." + calendar.get(Calendar.DATE);
        list.add(str);// ww  w  . j  a va 2  s. c  om
    }
    return list;
}

From source file:Main.java

public static ArrayList<String> getCurrentWeek() {
    SimpleDateFormat format = new SimpleDateFormat("dd");
    Calendar calendar = Calendar.getInstance();
    calendar.setFirstDayOfWeek(Calendar.SUNDAY);
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

    ArrayList<String> days = new ArrayList<String>();
    for (int i = 0; i < 7; i++) {
        days.add(format.format(calendar.getTime()));
        calendar.add(Calendar.DAY_OF_MONTH, 1);
    }/* w ww.  ja  v a 2  s.c o m*/
    return days;
}