Example usage for java.util Calendar getInstance

List of usage examples for java.util Calendar getInstance

Introduction

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

Prototype

public static Calendar getInstance() 

Source Link

Document

Gets a calendar using the default time zone and locale.

Usage

From source file:MainClass.java

public static void main(String[] args) {

    // get a calendar using the default time zone and locale.
    Calendar calendar = Calendar.getInstance();

    // set Date portion to January 1, 1970
    calendar.set(Calendar.YEAR, 1970);
    calendar.set(Calendar.MONTH, Calendar.JANUARY);
    calendar.set(Calendar.DATE, 1);

    // normalize the object
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    java.sql.Date javaSqlDate = new java.sql.Date(calendar.getTime().getTime());
}

From source file:MainClass.java

public static void main(String args[]) {
    Formatter fmt = new Formatter();
    Calendar cal = Calendar.getInstance();

    // Display just hour and minute. 
    fmt = new Formatter();
    fmt.format("%tl:%tM", cal, cal);
    System.out.println(fmt);/*from   ww w .  j  av a  2s  .com*/

}

From source file:Main.java

public static void main(String[] args) {
    String dayNames[] = new DateFormatSymbols().getWeekdays();
    Calendar date2 = Calendar.getInstance();
    System.out.println("Today is a " + dayNames[date2.get(Calendar.DAY_OF_WEEK)]);
}

From source file:Main.java

public static void main(String args[]) {
    Formatter fmt = new Formatter();
    Calendar cal = Calendar.getInstance();
    fmt = new Formatter();
    fmt.format("Default locale: %tc\n", cal);

    fmt.format(Locale.GERMAN, "For Locale.GERMAN: %tc\n", cal);
    fmt.format(Locale.ITALY, "For Locale.ITALY: %tc\n", cal);
    fmt.format(Locale.FRANCE, "For Locale.FRANCE: %tc\n", cal);
    System.out.println(fmt);//w  ww .j a  va2 s  .  com
}

From source file:MainClass.java

public static void main(String args[]) {
    Formatter fmt = new Formatter();
    Calendar cal = Calendar.getInstance();

    // Display month by name and number. 
    fmt = new Formatter();
    fmt.format("%tB %tb %tm", cal, cal, cal);
    System.out.println(fmt);//from  w  w  w  . jav a  2  s . c o  m
}

From source file:Main.java

public static void main(String args[]) {
    Calendar now = Calendar.getInstance();
    Locale locale = Locale.getDefault();

    Map<String, Integer> names = now.getDisplayNames(Calendar.DAY_OF_WEEK, Calendar.LONG, locale);
    SortedMap<String, Integer> nav = new TreeMap<String, Integer>(names);
    System.out.printf("Whole list:%n%s%n", nav);
    System.out.printf("First key: %s\tFirst entry: %s%n", nav.firstKey(), nav.firstEntry());
}

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--;/*from   ww w. j  a va 2s .c o  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");
    }
}

From source file:Main.java

public static void main(String... args) {

    SimpleDateFormat df = new SimpleDateFormat("MM/dd/YYYY");

    Calendar c = Calendar.getInstance();
    System.out.println(df.format(c.getTime()));

    c.add(Calendar.YEAR, 60);/*from  www.  ja  va2s.  c  o  m*/
    System.out.println(df.format(c.getTime()));
}

From source file:Main.java

public static void main(String args[]) {
    Calendar now = Calendar.getInstance();
    Locale locale = Locale.getDefault();

    Map<String, Integer> names = now.getDisplayNames(Calendar.DAY_OF_WEEK, Calendar.LONG, locale);
    NavigableMap<String, Integer> nav = new TreeMap<String, Integer>(names);
    System.out.printf("Whole list:%n%s%n", nav);
    System.out.printf("Key higher after Sunday: %s%n", nav.higherKey("Sunday"));
}

From source file:Main.java

public static void main(String args[]) {
    Calendar now = Calendar.getInstance();
    Locale locale = Locale.getDefault();

    Map<String, Integer> names = now.getDisplayNames(Calendar.DAY_OF_WEEK, Calendar.LONG, locale);
    NavigableMap<String, Integer> nav = new TreeMap<String, Integer>(names);
    System.out.printf("Whole list:%n%s%n", nav);
    System.out.printf("Map before Sunday: %s%n", nav.navigableKeySet());
}