Example usage for java.util GregorianCalendar GregorianCalendar

List of usage examples for java.util GregorianCalendar GregorianCalendar

Introduction

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

Prototype

public GregorianCalendar() 

Source Link

Document

Constructs a default GregorianCalendar using the current time in the default time zone with the default Locale.Category#FORMAT FORMAT locale.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    GregorianCalendar gc = new GregorianCalendar();
    int year = gc.get(Calendar.YEAR);
    System.out.println(year);/*from   w w w  .java2 s  .co m*/
    System.out.println(gc.isLeapYear(year));
    System.out.println("Month = " + gc.get(Calendar.MONTH));
    System.out.println("Week of year = " + gc.get(Calendar.WEEK_OF_YEAR));
    System.out.println("Week of month = " + gc.get(Calendar.WEEK_OF_MONTH));
    System.out.println("Day of year = " + gc.get(Calendar.DAY_OF_YEAR));
    System.out.println("Day of week = " + gc.get(Calendar.DAY_OF_WEEK));
}

From source file:GregorianCalendarDemo.java

public static void main(String args[]) {
    String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
    int year;/*from   www  .java  2 s  . c o m*/

    GregorianCalendar gcalendar = new GregorianCalendar();

    System.out.print("Date: ");
    System.out.print(months[gcalendar.get(Calendar.MONTH)]);
    System.out.print(" " + gcalendar.get(Calendar.DATE) + " ");
    System.out.println(year = gcalendar.get(Calendar.YEAR));

    System.out.print("Time: ");
    System.out.print(gcalendar.get(Calendar.HOUR) + ":");
    System.out.print(gcalendar.get(Calendar.MINUTE) + ":");
    System.out.println(gcalendar.get(Calendar.SECOND));

    if (gcalendar.isLeapYear(year)) {
        System.out.println("The current year is a leap year");
    } else {
        System.out.println("The current year is not a leap year");
    }
}

From source file:MainClass.java

public static void main(String[] args) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    GregorianCalendar gc = new GregorianCalendar();
    java.util.Date d = sdf.parse("12/12/2003");
    gc.setTime(d);/*  w  ww .ja  v  a2 s  . c  o  m*/
    System.out.println("Input Date = " + sdf.format(d));
    int dayBefore = gc.get(Calendar.DAY_OF_YEAR);
    gc.roll(Calendar.DAY_OF_YEAR, -1);
    int dayAfter = gc.get(Calendar.DAY_OF_YEAR);
    if (dayAfter > dayBefore) {
        gc.roll(Calendar.YEAR, -1);
    }
    gc.get(Calendar.DATE);
    java.util.Date yesterday = gc.getTime();
    System.out.println("Yesterdays Date = " + sdf.format(yesterday));

}

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[]) {
    int days = 1;
    int month = 1;
    int year = 2001;
    SimpleDateFormat sdf = new SimpleDateFormat("E dd-MM-yyyy G");
    StringBuffer buf = new StringBuffer();
    Calendar cal = new GregorianCalendar();
    cal.set(year, month - 1, days);/*from  www  . j a v a  2  s  . c  o  m*/
    sdf.format(cal.getTime(), buf, new FieldPosition(10));
    System.out.println(buf.toString());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Calendar calendar = new GregorianCalendar();
    calendar.set(Calendar.HOUR_OF_DAY, 13); // 1pm

    SpinnerDateModel dateModel = new SpinnerDateModel(calendar.getTime(), null, null, Calendar.HOUR_OF_DAY);
    JSpinner spinner = new JSpinner(dateModel);

    JFormattedTextField tf = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();
    DefaultFormatterFactory factory = (DefaultFormatterFactory) tf.getFormatterFactory();
    DateFormatter formatter = (DateFormatter) factory.getDefaultFormatter();

    // Change the date format to only show the hours
    formatter.setFormat(new SimpleDateFormat("hh:00 a"));
    //formatter.setFormat(new SimpleDateFormat("HH:00 a"));
}

From source file:Main.java

public static void main(String[] argv) {

    Instant instantFromDate = new Date().toInstant();

    Instant instantFromCalendar = Calendar.getInstance().toInstant();
    System.out.println(instantFromCalendar);

    ZoneId zoneId = java.util.TimeZone.getDefault().toZoneId();
    System.out.println(zoneId);//from   w w  w . j a v a  2  s.co m

    ZonedDateTime zonedDateTimeFromGregorianCalendar = new GregorianCalendar().toZonedDateTime();

    System.out.println(zonedDateTimeFromGregorianCalendar);
}

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  a va2  s  .  c  om*/
    new Timer().scheduleAtFixedRate(new test1(), start.getTime(), DateUtils.MILLIS_PER_MINUTE);
}

From source file:DateComboBoxRenderer.java

public static void main(String[] str) {
    JComboBox combo = new JComboBox();
    GregorianCalendar calendar = new GregorianCalendar();
    combo.addItem(calendar.getTime());/*w w w  . j a v a  2  s  .co  m*/

    calendar.roll(GregorianCalendar.DAY_OF_MONTH, 1);
    combo.addItem(calendar.getTime());

    combo.setRenderer(new DateComboBoxRenderer());

    JFrame frame = new JFrame();

    JPanel panel = new JPanel();
    panel.add(new JLabel("Date Combo: "));
    panel.add(combo);

    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}

From source file:BasicTimer.java

public static void main(String[] args) throws Exception {
    int type = TYPE_REMAINING;
    BasicTimer timer = new BasicTimer(type);
    timer.setMax(10000);/*from   w ww . j a v a 2s  .  c o m*/
    GregorianCalendar calendar = new GregorianCalendar();
    while (true) {
        Thread.sleep(1000);
        if (type == TYPE_CLOCK) {
            calendar.setTimeInMillis(timer.getTime());
            System.out.println("Clock: " + calendar);
        } else if (type == TYPE_ELAPSED) {
            System.out.println("Elapsed: " + (timer.getTime() / 1000) + " seconds");
        } else if (type == TYPE_REMAINING) {
            System.out.println("Remaining: " + (timer.getTime() / 1000) + " seconds");
        }
    }
}