Example usage for java.util GregorianCalendar get

List of usage examples for java.util GregorianCalendar get

Introduction

In this page you can find the example usage for java.util GregorianCalendar 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[] a) {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
    int day = calendar.get(Calendar.DAY_OF_WEEK);
    switch (day) {
    case Calendar.MONDAY:
        System.out.println(Calendar.MONDAY);
        break;//w  ww.ja v a2  s .  c  o  m
    case Calendar.TUESDAY:
        System.out.println(Calendar.TUESDAY);
        break;
    default:
        System.out.println("others");
    }
}

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 w w  w. j a  va2  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:org.squale.squaleexport.main.MapObjToXml.java

/**
 * @param args//from w  ww  . ja v  a  2 s.com
 */
public static void main(String[] args) {
    GregorianCalendar cal = new GregorianCalendar();
    System.out.println("launch@" + cal.get(Calendar.HOUR_OF_DAY) + "H" + cal.get(Calendar.MINUTE) + "min "
            + cal.get(Calendar.SECOND) + "s");
    String rootPath = System.getProperty("user.dir");

    String configFile = "/src/test/config/providers-config.xml";

    Initializer init = new Initializer(rootPath, configFile);
    init.initialize();
    // Maintenant que le socle JRAF est initialis, on peut crer un logger
    mLOGGER = LogFactory.getLog(MapObjToXml.class);

    Launch launch = new Launch();
    cal = new GregorianCalendar();
    System.out.println("start@" + cal.get(Calendar.HOUR_OF_DAY) + "H" + cal.get(Calendar.MINUTE) + "min "
            + cal.get(Calendar.SECOND) + "s");
    launch.exec();
    cal = new GregorianCalendar();
    System.out.println("finish@" + cal.get(Calendar.HOUR_OF_DAY) + "H" + cal.get(Calendar.MINUTE) + "min "
            + cal.get(Calendar.SECOND) + "s");
}

From source file:Main.java

public static void main(String[] args) {

    GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();

    // check if it is a leap year
    boolean isLeapYear = cal.isLeapYear(cal.get(GregorianCalendar.YEAR));
    System.out.println("Is leap year:" + isLeapYear);

    // check if 2014 is a leap year
    isLeapYear = cal.isLeapYear(2014);/*from   w ww  .j  a  v  a2 s.  co  m*/
    System.out.println("Is leap year:" + isLeapYear);

}

From source file:MainClass.java

public static void main(String[] a) {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
    calendar.add(Calendar.YEAR, -14);
    System.out.println(calendar.get(Calendar.YEAR));
}

From source file:CalendarTest.java

public static void main(String[] args) {
    // construct d as current date
    GregorianCalendar d = new GregorianCalendar();

    int today = d.get(Calendar.DAY_OF_MONTH);
    int month = d.get(Calendar.MONTH);

    // set d to start date of the month
    d.set(Calendar.DAY_OF_MONTH, 1);

    int weekday = d.get(Calendar.DAY_OF_WEEK);

    // get first day of week (Sunday in the U.S.)
    int firstDayOfWeek = d.getFirstDayOfWeek();

    // determine the required indentation for the first line
    int indent = 0;
    while (weekday != firstDayOfWeek) {
        indent++;//from  w  w w  .jav a 2 s.c o  m
        d.add(Calendar.DAY_OF_MONTH, -1);
        weekday = d.get(Calendar.DAY_OF_WEEK);
    }

    // print weekday names
    String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();
    do {
        System.out.printf("%4s", weekdayNames[weekday]);
        d.add(Calendar.DAY_OF_MONTH, 1);
        weekday = d.get(Calendar.DAY_OF_WEEK);
    } while (weekday != firstDayOfWeek);
    System.out.println();

    for (int i = 1; i <= indent; i++)
        System.out.print("    ");

    d.set(Calendar.DAY_OF_MONTH, 1);
    do {
        // print day
        int day = d.get(Calendar.DAY_OF_MONTH);
        System.out.printf("%3d", day);

        // mark current day with *
        if (day == today)
            System.out.print("*");
        else
            System.out.print(" ");

        // advance d to the next day
        d.add(Calendar.DAY_OF_MONTH, 1);
        weekday = d.get(Calendar.DAY_OF_WEEK);

        // start a new line at the start of the week
        if (weekday == firstDayOfWeek)
            System.out.println();
    } while (d.get(Calendar.MONTH) == month);
    // the loop exits when d is day 1 of the next month

    // print final end of line if necessary
    if (weekday != firstDayOfWeek)
        System.out.println();
}

From source file:MainClass.java

public static void main(String[] a) {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
    calendar.add(Calendar.YEAR, 14); // 14 years into the future
    System.out.println(calendar.get(Calendar.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);//  ww w  . j  a  v  a 2s  .  com
    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:Main.java

public static void main(String[] argv) throws Exception {

    GregorianCalendar gc = new GregorianCalendar();
    gc.setLenient(false);/* w ww  .j a  va  2s  . c o m*/
    gc.set(GregorianCalendar.YEAR, 2003);
    gc.set(GregorianCalendar.MONTH, 12);
    gc.set(GregorianCalendar.DATE, 1);

    int m = gc.get(GregorianCalendar.MONTH) + 1;
    int d = gc.get(GregorianCalendar.DATE);
    String mm = Integer.toString(m);
    String dd = Integer.toString(d);
    System.out.println(gc.get(GregorianCalendar.YEAR) + (m < 10 ? "0" + mm : mm) + (d < 10 ? "0" + dd : dd));
}

From source file:io.pcp.parfait.dxm.PcpMmvWriter.java

public static void main(String[] args) throws IOException {
    PcpMmvWriter bridge;/*from   w  w  w. j  a v a  2  s . com*/

    if (args.length == 0) {
        // use $PCP_PMDAS_DIR/mmv/mmvdump (no args) as diagnostic tool
        bridge = new PcpMmvWriter("test", IdentifierSourceSet.DEFAULT_SET);
    } else {
        bridge = new PcpMmvWriter(new File(args[0]), IdentifierSourceSet.DEFAULT_SET);
    }

    // Automatically uses default int handler
    bridge.addMetric(MetricName.parse("sheep[baabaablack].bagsfull.count"), Semantics.COUNTER,
            ONE.multiply(1000), 3);

    // Automatically uses default boolean-to-int handler
    bridge.addMetric(MetricName.parse("sheep[baabaablack].bagsfull.haveany"), Semantics.INSTANT, null,
            new AtomicBoolean(true));
    bridge.addMetric(MetricName.parse("sheep[limpy].bagsfull.haveany"), Semantics.INSTANT, null,
            new AtomicBoolean(false));

    // Automatically uses default long handler
    bridge.addMetric(MetricName.parse("sheep[insomniac].jumps"), Semantics.COUNTER, ONE, 12345678901234L);

    // Automatically uses default double handler
    bridge.addMetric(MetricName.parse("sheep[limpy].legs.available"), Semantics.DISCRETE, ONE, 0.75);

    // Uses this class' custom String handler
    bridge.addMetric(MetricName.parse("sheep[limpy].jumpitem"), Semantics.DISCRETE, null, "fence");

    // addMetric(GregorianCalendar) would fail, as there's no handler registered by default for
    // GregorianCalendars; use a custom one which puts the year as an int
    bridge.addMetric(MetricName.parse("sheep[insomniac].lastjumped"), Semantics.INSTANT, null,
            new GregorianCalendar(), new AbstractTypeHandler<GregorianCalendar>(MmvMetricType.I32, 4) {
                public void putBytes(ByteBuffer buffer, GregorianCalendar value) {
                    buffer.putInt(value.get(GregorianCalendar.YEAR));
                }
            });

    // addMetric(Date) would fail, as there's no handler registered; register one for all date
    // types from now on
    bridge.registerType(Date.class, new AbstractTypeHandler<Date>(MmvMetricType.I64, 8) {
        public void putBytes(ByteBuffer buffer, Date value) {
            buffer.putLong(value.getTime());
        }
    });
    // These will both use the handler we just registered
    bridge.addMetric(MetricName.parse("cow.how.now"), Semantics.INSTANT, null, new Date());
    bridge.addMetric(MetricName.parse("cow.how.then"), Semantics.INSTANT, null,
            new GregorianCalendar(1990, 1, 1, 12, 34, 56).getTime());

    // Uses units
    bridge.addMetric(MetricName.parse("cow.bytes.total"), Semantics.COUNTER, BYTE, 10000001);
    bridge.addMetric(MetricName.parse("cow.bytes.rate"), Semantics.INSTANT, BYTE.multiply(1024).divide(SECOND),
            new Date());
    bridge.addMetric(MetricName.parse("cow.bytes.chewtime"), Semantics.INSTANT, HOUR.divide(BYTE), 7);
    bridge.addMetric(MetricName.parse("cow.bytes.jawmotion"), Semantics.INSTANT, KILO(HERTZ), 0.5);

    // Set up some help text
    bridge.setInstanceDomainHelpText("sheep", "sheep in the paddock",
            "List of all the sheep in the paddock. Includes 'baabaablack', 'insomniac' (who likes to jump fences), and 'limpy' the three-legged wonder sheep.");
    bridge.setMetricHelpText("sheep.jumps", "# of jumps done",
            "Number of times the sheep has jumped over its jumpitem");

    // All the metrics are added; write the file
    bridge.start();
    // Metrics are visible to the agent from this point on

    // Sold a bag! Better update the count
    bridge.updateMetric(MetricName.parse("sheep[baabaablack].bagsfull.count"), 2);
    // The fence broke! Need something new to jump over
    bridge.updateMetric(MetricName.parse("sheep[limpy].jumpitem"), "Honda Civic");
    // Values will be reflected in the agent immediately
}