How Date, GregorianCalendar, and TimeZone objects are created and how to use their methods to access date/time information. : Date « Utility Classes « SCJP

Home
SCJP
1.Java Source And Data Type
2.Operators
3.Modifiers
4.Type Casting
5.Statements
6.Object Oriented
7.Thread
8.Utility Classes
9.File
SCJP » Utility Classes » Date 
8.26.1.How Date, GregorianCalendar, and TimeZone objects are created and how to use their methods to access date/time information.
import java.util.*;
   
public class MainClass{
 public static void main(String args[]){
  Date today = new Date();
  GregorianCalendar cal = new GregorianCalendar();
  cal.setTime(today);
  System.out.println("Today: ");
  displayDateInfo(cal);
  cal.clear();
  cal.set(2000,0,1);
  System.out.println("\nNew Years Day 2000: ");
  displayDateInfo(cal);
 }
 static void displayDateInfo(GregorianCalendar cal){
  String days[] {"","Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
  String months[] {"January","February","March","April","May",
   "June","July","August","September","October","November",
   "December"};
  String am_pm[] {"AM","PM"};
  System.out.println("Year: "+cal.get(Calendar.YEAR));
  System.out.println("Month: "+months[cal.get(Calendar.MONTH)]);
  System.out.println("Date: "+cal.get(Calendar.DATE));
  System.out.println("Day: "+days[cal.get(Calendar.DAY_OF_WEEK)]);
  System.out.println("Hour: "+(cal.get(Calendar.HOUR)+12)%13);
  System.out.println("Minute: "+cal.get(Calendar.MINUTE));
  System.out.println("Second: "+cal.get(Calendar.SECOND));
  System.out.println(am_pm[cal.get(Calendar.AM_PM)]);
  TimeZone tz=cal.getTimeZone();
  System.out.println("Time Zone: "+tz.getID());
 }
}
Today: 
Year: 2010
Month: April
Date: 12
Day: Mon
Hour: 7
Minute: 49
Second: 38
AM
Time Zone: America/Los_Angeles

New Years Day 2000: 
Year: 2000
Month: January
Date: 1
Day: Sat
Hour: 12
Minute: 0
Second: 0
AM
Time Zone: America/Los_Angeles
8.26.Date
8.26.1.How Date, GregorianCalendar, and TimeZone objects are created and how to use their methods to access date/time information.
8.26.2.Get the current date and time.
8.26.3.Date and time calculations, and format date in different locales with different date styles.
8.26.4.Find out how long it took for a trillion milliseconds to pass, starting at January 1, 1970
8.26.5.The Calendar Class
8.26.6.The DateFormat Class
8.26.7.DateFormat.format(), DateFormat.parse()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.