1. What is the time zone first?
Time zone is the same time definition used for the region on Earth. When the international Longitude Conference was held in Washington in 1884, in order to overcome the chaos of time, the world was divided into 24 time zones.
2. Date
The TimeZone object gives us the original offset, which is the number of microseconds from GMT, and the Java Date Object stores the number of milliseconds that elapsed from the current moment to January 1, 1970 0:00, which is not related to the time zone and the region (in fact it can be considered GMT time)
The time of the computer's internal record (date date = new Date ()), Greenwich Mean Time (GMT). That is, Java.util.Date represents a point in time, and its value is the number of milliseconds from 00:00:00 A.D. January 1, 1970, so it can be considered as having no time zone and locale concepts.
3, DateFormat
Date formatting class DateFormat, for different regions of the configuration generally have two points, one is the locale, one is timezone
The former (Locale) enables DateFormat to output text according to the configured locale characteristics (e.g. China, United States, France different regions of the date are not the same format, China may be October 5, 2001)
The latter (TimeZone) lets DateFormat know how to convert and adjust the time offset to get the time of the configured timezone.
Assuming the current time zone is gmt+0, get the current 2:00, then if you configure Dateformat.settimezome ("Gmt+8"), that is, the time zone of Beijing, then the format output is 10:00, Because the system has a time offset adjustment to the original number of milliseconds (to the time zone you set), that is, add more than 8 hours, and then format the output date as a string
4. Code examples
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.Date;
public class Test {
public static void main (String [] args) {
//TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai "));
//TimeZone.setDefault(TimeZone.getTimeZone("Europe/Paris ")); // Note the code below this line to uncomment
TimeZone time = TimeZone.getDefault (); // This is used for internationalization
System.out.println (time);
Calendar calendar = Calendar.getInstance (); // Get instance
DateFormat format1 = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); // Construct a formatting template
Date date = calendar.getTime (); // Get Date object
String str = new String ();
str = format1.format (date); // The object is formatted to get the output in string format
System.out.println (str);
}
}
The result of the output is:
Sun.util.calendar.zoneinfo[id= "Asia/shanghai", offset=28800000,dstsavings=0,usedaylight=false, transitions=19,lastrule=null]2015-07-03 22:27:14
If you open the comment europe/Paris Line, the result of the operation is:
Sun.util.calendar.zoneinfo[id= "Europe/paris", offset=3600000,dstsavings=3600000,usedaylight=true, transitions=184,lastrule=java.util.simpletimezone[id=europe/paris,offset=3600000,dstsavings=3600000, Usedaylight=True, startyear=0,startmode=2,startmonth=2,startday=-1,startdayofweek=1,starttime=3600000, starttimemode=2,endmode=2,endmonth=9,endday=-1,enddayofweek=1,endtime=3600000,endtimemode=2]] 2015-07-03 16:30:49
Description use the Timezone.setdefault method to adjust the displayed time zone
Java&postgresql Time Zone Summary