Example usage for java.util Calendar setTime

List of usage examples for java.util Calendar setTime

Introduction

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

Prototype

public final void setTime(Date date) 

Source Link

Document

Sets this Calendar's time with the given Date.

Usage

From source file:Main.java

public static void main(String[] args) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
}

From source file:Main.java

public static void main(String[] args) {
    Date date = Date.valueOf("2015-12-25");
    System.out.println(date);/*  w  ww  . j  ava 2 s  . c  om*/

    Calendar cal = new GregorianCalendar();
    cal.setTime(date);
    System.out.println(cal.getTime());
}

From source file:Main.java

public static void main(String[] args) {

    Date today = new Date();

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(today);

    calendar.add(Calendar.MONTH, 1);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.add(Calendar.DATE, -1);

    Date lastDayOfMonth = calendar.getTime();

    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    System.out.println("Today            : " + sdf.format(today));
    System.out.println("Last Day of Month: " + sdf.format(lastDayOfMonth));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
    Date workDate = simpleDateFormat1.parse("2011-11-27");

    Calendar workCalendar = Calendar.getInstance();
    workCalendar.setTime(workDate);
    SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("HH:mm:ss");
    Calendar time = Calendar.getInstance();
    time.setTime(simpleDateFormat2.parse("06:00:00"));
    workCalendar.set(Calendar.HOUR_OF_DAY, time.get(Calendar.HOUR_OF_DAY));
    workCalendar.set(Calendar.MINUTE, time.get(Calendar.MINUTE));
    workCalendar.set(Calendar.SECOND, time.get(Calendar.SECOND));

    Date newWorkDate = workCalendar.getTime();

    SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    System.out.println(simpleDateFormat3.format(newWorkDate));
}

From source file:Main.java

public static void main(String[] args) {
    String input = "2014-05-04 09:10:40.321";

    // Prepare the pattern
    String pattern = "yyyy-MM-dd HH:mm:ss.SSS";

    SimpleDateFormat sdf = new SimpleDateFormat(pattern);

    // Parse the text into a Date object
    Date dt = sdf.parse(input, new ParsePosition(0));
    System.out.println(dt);/*from   w  w w .j a v  a 2  s  .co  m*/

    // Get the Calendar instance
    Calendar cal = Calendar.getInstance();
    cal.setTime(dt);

    // Print time parts
    System.out.println("Hour:" + cal.get(Calendar.HOUR));
    System.out.println("Minute:" + cal.get(Calendar.MINUTE));
    System.out.println("Second:" + cal.get(Calendar.SECOND));
    System.out.println("Millisecond:" + cal.get(Calendar.MILLISECOND));

}

From source file:MainClass.java

public static void main(String[] a) {
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(new Date());

    System.out.println("ERA: " + calendar.get(Calendar.ERA));
    System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
    System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
    System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
    System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
    System.out.println("DATE: " + calendar.get(Calendar.DATE));
    System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
    System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
    System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
    System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
    System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
    System.out.println("ZONE_OFFSET: " + (calendar.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000)));
    System.out.println("DST_OFFSET: " + (calendar.get(Calendar.DST_OFFSET) / (60 * 60 * 1000)));
}

From source file:com.pureinfo.srm.config.notice.TestTimer.java

public static void main(String[] args) throws ParseException {
    Calendar start = new GregorianCalendar();
    start.setTime(format.parse("18:40"));
    Calendar cal = new GregorianCalendar();
    start.set(Calendar.YEAR, cal.get(Calendar.YEAR));
    start.set(Calendar.MONTH, cal.get(Calendar.MONTH));
    start.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH));
    while (start.before(cal)) {
        start.setTimeInMillis(start.getTimeInMillis() + DateUtils.MILLIS_PER_MINUTE);
    }/*w w  w. j  av  a  2s  . c  o  m*/
    new Timer().scheduleAtFixedRate(new test1(), start.getTime(), DateUtils.MILLIS_PER_MINUTE);
}

From source file:KVMCalendar.java

public static void main(String[] args) {
    Calendar cal = Calendar.getInstance();
    Date date = new Date();
    cal.setTime(date);
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DAY_OF_MONTH);
    System.out.println("Day is " + day + ", month is " + month);

    final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000L;
    long offset = date.getTime();
    offset += 20 * MILLIS_PER_DAY;// www  .  j  a  va  2  s . c  o  m
    date.setTime(offset);
    cal.setTime(date);

    month = cal.get(Calendar.MONTH);
    day = cal.get(Calendar.DAY_OF_MONTH);
    System.out.println("In 20 days time, day will " + day + ", month will be " + month);
    System.out.println(cal);
}

From source file:MainClass.java

public static void main(String args[]) {
    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);
    int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
    if (dayofweek == Calendar.SUNDAY)
        System.out.println("SUNDAY");
    if (dayofweek == Calendar.MONDAY)
        System.out.println("MONDAY");
    if (dayofweek == Calendar.TUESDAY)
        System.out.println("TUESDAY");
    if (dayofweek == Calendar.WEDNESDAY)
        System.out.println("WEDNESDAY");
    if (dayofweek == Calendar.THURSDAY)
        System.out.println("THURSDAY");
    if (dayofweek == Calendar.FRIDAY)
        System.out.println("FRIDAY");
    if (dayofweek == Calendar.SATURDAY) {
        System.out.println("SATURDAY");
    }//from   w  w  w.  j a va  2 s .c  o  m
}

From source file:Main.java

public static void main(String[] args) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());

    calendar.set(Calendar.DAY_OF_MONTH, 1);
    // day of week for first date of month
    int weekOfFirstDate = calendar.get(Calendar.WEEK_OF_YEAR);

    int lastDateOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    calendar.set(Calendar.DAY_OF_MONTH, lastDateOfMonth);
    // day of week for last date of month

    int weekOfLastDate = calendar.get(Calendar.WEEK_OF_YEAR);

    calendar.roll(Calendar.MONTH, false);
    int lastDateOfPrevMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

    int weeksToDisplay = weekOfLastDate - weekOfFirstDate + 1;
    int[] days = new int[weeksToDisplay * 7];

    int firstDayPosition = 3;

    // fill previous month
    int x = lastDateOfPrevMonth;
    for (int i = firstDayPosition - 1; i >= 0; i--) {
        days[i] = x--;/*ww  w . ja  va2 s.co  m*/
    }

    // fill current month
    for (int i = 1; i < lastDateOfMonth + 1; i++) {
        days[firstDayPosition - 1 + i] = i;
    }

    // fill next month
    int j = 1;
    for (int i = lastDateOfMonth + firstDayPosition; i < days.length; i++) {
        days[i] = j++;
    }

    for (int i = 0; i < days.length; i++) {
        if (i % 7 == 0) {
            System.out.println();
        }
        System.out.print(days[i] + "\t");
    }
}