The JAVA2 platform provides us with a rich date-time API. such as Java.util.date;java.util.calendar;java.text.dateformat and so on. So what's the relationship between them?
First, Java.util.Date represents a point-in-time value that is the number of milliseconds from 00:00:00 A.D. January 1, 1970. So it's not a time zone or locale concept. Java obtains the current point in time by the following form:
Date now = new Date (); This point of time is independent of the local system's time zone
And because of its independence from the time zone, it makes our storage data (time) consistent (timezone consistency). In general, we will now be stored in the database, when we need to show the data, now formatted to the desired format, such as: 2011-11-19 14:12:23. And this function is generally referred to Java.text.DateFormat to achieve. For example:
SimpleDateFormat SDF =new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String snow = Sdf.format (now); // 2011-11-19 14:12:23
We find that snow is a string with time (14:12:23), and we cannot help asking, which time zone is this time (14:12:23)? By default,SimpleDateFormat takes the local system's time zone (my time zone is gmt+8 Beijing) and then formats now with the pattern ("Yyyy-mm-dd HH:mm:ss"), which is the time to output the gmt+8 zone. . If you want to support internationalization time, specify the time zone before formatting the date data. For example:
SimpleDateFormat SDF =new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Sdf.settimezone (Timezone.gettimezone ("gmt+8"));
String snow = Sdf.format (now); // 2011-11-19 14:12:23
Alternatively, you can modify the local time zone information by using the following code:
Timezone.setdefault (Timezone.gettimezone ("gmt+8"));
The Java.util.Calendar class also represents a point-in-time, but it is a date-facade tool class that provides a number of convenient ways to convert (calculate) time-to-year, month, day, hour, minute, second, and week.
Calendar calendar = calendar.getinstance (timezone);
Date d = calendar.gettime ();
Calendar calculations are also time-zone-based, for example: the number of hours in different time zones for the same date is not the same. But Calendar.gettime (); The returned date is not a time zone, because it is of type date. For example:
PublicStaticvoid Main (string[] args)Throws Interruptedexception {
Calendar Calendar1 = Calendar
. getinstance (Timezone.gettimezone ("gmt+8"));
Calendar CALENDAR2 = Calendar
. getinstance (Timezone.gettimezone ("gmt+1"));
System.out.println ("Millis =" + Calendar1.gettimeinmillis ());
System.out.println ("Millis =" + Calendar2.gettimeinmillis ());
System.out.println ("hour =" + Calendar1.get (calendar.hour));
System.out.println ("hour =" + Calendar2.get (calendar.hour));
System.out.println ("date =" + Calendar1.gettime ());
System.out.println ("date =" + Calendar2.gettime ());
}
Output:
Millis = 1358614681203
Millis = 1358614681203
hour = 3
hour = 8
Date = Thu 15:11:21 CST 2011
Date = Thu 15:11:21 CST 2011
The relationship between date and timezone