Example usage for java.util Calendar get

List of usage examples for java.util Calendar get

Introduction

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

Prototype

public int get(int field) 

Source Link

Document

Returns the value of the given calendar field.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Calendar local = Calendar.getInstance();
    Calendar japanCal = new GregorianCalendar(TimeZone.getTimeZone("Japan"));
    japanCal.setTimeInMillis(local.getTimeInMillis());

    int hour = japanCal.get(Calendar.HOUR); // 3
    int minutes = japanCal.get(Calendar.MINUTE); // 0
    int seconds = japanCal.get(Calendar.SECOND); // 0
    boolean am = japanCal.get(Calendar.AM_PM) == Calendar.AM; // true
}

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:Main.java

public static void main(String[] args) {

    Calendar cal = Calendar.getInstance();

    // get the days required in the first week of the year
    System.out.println("Months required: " + cal.get(Calendar.MONTH));

    // print the lowest maximum months for year.
    int max = cal.getLeastMaximum(Calendar.MONTH);
    System.out.println("Lowest maximum months :" + max);

}

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 ww  w.jav  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: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);/*w  w  w. ja va  2s  . c om*/
    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) {
    Calendar now = Calendar.getInstance();
    System.out.println(now.getTime());
    int month = now.get(Calendar.MONTH);
    if (month == Calendar.JANUARY) {
        System.out.println("JANUARY");
    }//w  ww.j a  va2s.co  m
    if (month == Calendar.FEBRUARY) {
        System.out.println("FEBRUARY");
    }
    if (month == Calendar.MARCH) {
        System.out.println("MARCH");
    }
    if (month == Calendar.APRIL) {
        System.out.println("APRIL");
    }
    if (month == Calendar.MAY) {
        System.out.println("MAY");
    }
    if (month == Calendar.JUNE) {
        System.out.println("JUNE");
    }
    if (month == Calendar.JULY) {
        System.out.println("JULY");
    }
    if (month == Calendar.AUGUST) {
        System.out.println("AUGUST");
    }
    if (month == Calendar.SEPTEMBER) {
        System.out.println("SEPTEMBER");
    }
    if (month == Calendar.OCTOBER) {
        System.out.println("OCTOBER");
    }
    if (month == Calendar.NOVEMBER) {
        System.out.println("NOVEMBER");
    }
    if (month == Calendar.DECEMBER) {
        System.out.println("DECEMBER");
    }

}

From source file:Main.java

public static void main(String[] args) {
    Calendar calNewYork = Calendar.getInstance();

    calNewYork.setTimeZone(TimeZone.getTimeZone("America/New_York"));

    System.out.println("Time in New York: " + calNewYork.get(Calendar.HOUR_OF_DAY) + ":"
            + calNewYork.get(Calendar.MINUTE));

}

From source file:Main.java

public static void main(String[] args) {
    Calendar calTokyo = Calendar.getInstance();

    calTokyo.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));

    System.out.println(/*from  ww  w. jav a 2 s  .  c o m*/
            "Time in Tokyo: " + calTokyo.get(Calendar.HOUR_OF_DAY) + ":" + calTokyo.get(Calendar.MINUTE));
}

From source file:Main.java

public static void main(String[] args) {
    Calendar calParis = Calendar.getInstance();

    calParis.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
    System.out.println(// ww  w  .j av  a  2 s .  co  m
            "Time in Paris: " + calParis.get(Calendar.HOUR_OF_DAY) + ":" + calParis.get(Calendar.MINUTE));

}

From source file:Main.java

public static void main(String[] args) {
    Calendar calendar = Calendar.getInstance();
    int lastDate = calendar.getActualMaximum(Calendar.DATE);

    calendar.set(Calendar.DATE, lastDate);
    int lastDay = calendar.get(Calendar.DAY_OF_WEEK);

    System.out.println("Last Date: " + calendar.getTime());

    System.out.println("Last Day : " + lastDay);
}