Java获得指定时区时间

本博客转载而来,转载自:https://blog.csdn.net/xuemengrui12/article/details/78218122

https://blog.csdn.net/qq_37524923/article/details/80323115

一、

在Java语言中,您可以通过java.util.Calendar类取得一个本地时间或者指定时区的时间实例,如下:

  1. // 取得本地时间:
  2. Calendar cal = Calendar.getInstance();
  3. //取得指定时区的时间:      
  4. TimeZone zone = TimeZone.getTimeZone(“GMT- 8: 00″);
  5. Calendar cal = Calendar.getInstance(zone);

或者:
[java]  view plain  copy
  1. <code class="language-java"><span style="font-size:18px;">Calendar cal = Calendar.getInstance(Locale.CHINA);</span></code>  
写几个实例:
  1. /**
  2. * 获得东八区时间
  3. *
  4. * @return
  5. */
  6. public static String getChinaTime() {
  7. TimeZone timeZone = TimeZone.getTimeZone( "GMT+8:00");
  8. SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
  9. simpleDateFormat.setTimeZone(timeZone);
  10. return simpleDateFormat.format( new Date());
  11. }

还有一种方式是先取得UTC时间,然后再转换为东八区时间
  1. /**
  2. * 得到UTC时间,类型为字符串,格式为"yyyy-MM-dd HH:mm"
  3. * 如果获取失败,返回null
  4. *
  5. * @return
  6. */
  7. public static String getUTCTimeStr() {
  8. SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
  9. StringBuffer UTCTimeBuffer = new StringBuffer();
  10. // 1、取得本地时间:
  11. Calendar cal = Calendar.getInstance();
  12. // 2、取得时间偏移量:
  13. int zoneOffset = cal.get(Calendar.ZONE_OFFSET);
  14. // 3、取得夏令时差:
  15. int dstOffset = cal.get(Calendar.DST_OFFSET);
  16. // 4、从本地时间里扣除这些差量,即可以取得UTC时间:
  17. cal.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
  18. int year = cal.get(Calendar.YEAR);
  19. int month = cal.get(Calendar.MONTH) + 1;
  20. int day = cal.get(Calendar.DAY_OF_MONTH);
  21. int hour = cal.get(Calendar.HOUR_OF_DAY);
  22. int minute = cal.get(Calendar.MINUTE);
  23. int second = cal.get(Calendar.SECOND);
  24. UTCTimeBuffer.append(year).append( "-").append(month).append( "-").append(day);
  25. UTCTimeBuffer.append( " ").append(hour).append( ":").append(minute).append( ":").append(second );
  26. try {
  27. format.parse(UTCTimeBuffer.toString());
  28. return UTCTimeBuffer.toString();
  29. } catch (ParseException e) {
  30. e.printStackTrace();
  31. }
  32. return null;
  33. }
  34. /**
  35. * 将UTC时间转换为东八区时间
  36. * @param UTCTime
  37. * @return
  38. */
  39. public static String getLocalTimeFromUTC(String UTCTime){
  40. Date UTCDate;
  41. SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
  42. String localTimeStr = null ;
  43. try {
  44. UTCDate = format.parse(UTCTime);
  45. format.setTimeZone(TimeZone.getTimeZone( "GMT-8")) ;
  46. localTimeStr = format.format(UTCDate) ;
  47. } catch (ParseException e) {
  48. e.printStackTrace();
  49. }
  50. return localTimeStr ;
  51. }

再来个更高级的,可以获得各个时区的时间
  1. /**
  2. * 获得任意时区的时间
  3. *
  4. * @param timeZoneOffset
  5. * @return
  6. */
  7. public static String getFormatedDateString(float timeZoneOffset) {
  8. if (timeZoneOffset > 13 || timeZoneOffset < - 12) {
  9. timeZoneOffset = 0;
  10. }
  11. int newTime = ( int) (timeZoneOffset * 60 * 60 * 1000);
  12. TimeZone timeZone;
  13. String[] ids = TimeZone.getAvailableIDs(newTime);
  14. if (ids.length == 0) {
  15. timeZone = TimeZone.getDefault();
  16. } else {
  17. timeZone = new SimpleTimeZone(newTime, ids[ 0]);
  18. }
  19. SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
  20. sdf.setTimeZone(timeZone);
  21. return sdf.format( new Date());
  22. }

二、

public static String getTimeZone(){
    Calendar cal = Calendar.getInstance();
    int offset = cal.get(Calendar.ZONE_OFFSET);
    cal.add(Calendar.MILLISECOND, -offset);
    Long timeStampUTC = cal.getTimeInMillis();
    Long timeStamp = System.currentTimeMillis();
    Long timeZone = (timeStamp - timeStampUTC) / (1000 * 3600); System.out.println(timeZone.intValue());
    return String.valueOf(timeZone+1);

}




参考:


猜你喜欢

转载自blog.csdn.net/meng19910117/article/details/80938030
今日推荐