Java gets time of each time zone, gets the current time to GMT January 01, 1970 00:00 00 seconds

Source: Internet
Author: User


    1. GMT is utc/gmt time, January 01, 1970 00:00 00 seconds (that is, utc+8 Beijing time January 01, 1970 08:00 00 seconds)
      The calculation code is as follows:
1  /**
 2 * Get the number of seconds from the specified time to Greenwich Mean Time
 3 * UTC: 01:00, 00:00, GMT, January 1, 1970 (UTC+8 Beijing time, January 01, 1970, 08:00:00)
 4 * @param time
 5 * @return
 6 */
 7 public static long diffSeconds(String time){
 8 Calendar calendar = Calendar.getInstance();
 9 
10 calendar.clear();
11 Date datetime = DatetimeUtil.toDateByDate14(time);
12 calendar.setTime(datetime);
13
14 TimeZone timeZone = TimeZone.getTimeZone("GMT+08:00");
15 calendar.setTimeZone(timeZone);
16 return calendar.getTimeInMillis()/1000;
17 }
18
19 public static void main(String[] args) throws Exception {
20
21 String datetime = DatetimeUtil.getDatetime();
22 System.out.println("================= Method One: calendar====================== =======");
23 System.out.println(diffSeconds(datetime));
24 System.out.println("================= Method 2: Calculate the time difference ===================== ========");
25 System.out.println(DatetimeUtil.diffSeconds("19700101080000", datetime, DatetimeUtil.PATTERN_YYYYMMDDHHMMSS));
26 System.out.println("================= Method 3: Using system===================== ========");
27 System.out.println(System.currentTimeMillis()/1000);
28 } 


2. use Java to take time in a specified time zone Beijing time, New York time, Bangalore time


1 /**
 2 * Take Beijing time
 3 * @return
 4 */
 5 public static String getBeijingTime(){
 6 return getFormatedDateString(8);
 7 }
 8     
 9     /**
10 * Take the time in Bangalore
11 * @return
12 */
13 public static String getBangaloreTime(){
14 return getFormatedDateString(5.5f);
15 }
16
17 /**
18 * Take New York time
19 * @return
20 */
21 public static String getNewyorkTime(){
22 return getFormatedDateString(-5);
twenty three     }
twenty four     
25 /**
26 * This function is non-original. Searching from the Internet, timeZoneOffset was originally int type, adjusted to float type for Bangalore
27 * timeZoneOffset indicates the time zone. For example, China generally uses the East 8th district, so timeZoneOffset is 8
28 * @param timeZoneOffset
29 * @return
30 */
31 public static String getFormatedDateString(float timeZoneOffset){
32 if (timeZoneOffset > 13 || timeZoneOffset < -12) {
33 timeZoneOffset = 0;
34 }
35
36 int newTime=(int)(timeZoneOffset * 60 * 60 * 1000);
37 TimeZone timeZone;
38 String[] ids = TimeZone.getAvailableIDs(newTime);
39 if (ids.length == 0) {
40 timeZone = TimeZone.getDefault();
41 } else {
42 timeZone = new SimpleTimeZone(newTime, ids[0]);
43 }
44
45 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
46 sdf.setTimeZone(timeZone);
47 return sdf.format(new Date());
48 }



Java gets time of each time zone, gets the current time to GMT January 01, 1970 00:00 00 seconds


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.