本博客转载而来,转载自:https://blog.csdn.net/xuemengrui12/article/details/78218122
https://blog.csdn.net/qq_37524923/article/details/80323115
一、
在Java语言中,您可以通过java.util.Calendar类取得一个本地时间或者指定时区的时间实例,如下:
-
// 取得本地时间:
-
Calendar cal = Calendar.getInstance();
-
//取得指定时区的时间:
-
TimeZone zone = TimeZone.getTimeZone(“GMT- 8: 00″);
-
Calendar cal = Calendar.getInstance(zone);
或者:
- <code class="language-java"><span style="font-size:18px;">Calendar cal = Calendar.getInstance(Locale.CHINA);</span></code>
-
/**
-
* 获得东八区时间
-
*
-
* @return
-
*/
-
public static String getChinaTime() {
-
TimeZone timeZone = TimeZone.getTimeZone( "GMT+8:00");
-
SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
-
simpleDateFormat.setTimeZone(timeZone);
-
return simpleDateFormat.format( new Date());
-
}
还有一种方式是先取得UTC时间,然后再转换为东八区时间
-
/**
-
* 得到UTC时间,类型为字符串,格式为"yyyy-MM-dd HH:mm"
-
* 如果获取失败,返回null
-
*
-
* @return
-
*/
-
public static String getUTCTimeStr() {
-
SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
-
-
StringBuffer UTCTimeBuffer = new StringBuffer();
-
// 1、取得本地时间:
-
Calendar cal = Calendar.getInstance();
-
// 2、取得时间偏移量:
-
int zoneOffset = cal.get(Calendar.ZONE_OFFSET);
-
// 3、取得夏令时差:
-
int dstOffset = cal.get(Calendar.DST_OFFSET);
-
// 4、从本地时间里扣除这些差量,即可以取得UTC时间:
-
cal.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
-
int year = cal.get(Calendar.YEAR);
-
int month = cal.get(Calendar.MONTH) + 1;
-
int day = cal.get(Calendar.DAY_OF_MONTH);
-
int hour = cal.get(Calendar.HOUR_OF_DAY);
-
int minute = cal.get(Calendar.MINUTE);
-
int second = cal.get(Calendar.SECOND);
-
UTCTimeBuffer.append(year).append( "-").append(month).append( "-").append(day);
-
UTCTimeBuffer.append( " ").append(hour).append( ":").append(minute).append( ":").append(second );
-
try {
-
format.parse(UTCTimeBuffer.toString());
-
return UTCTimeBuffer.toString();
-
} catch (ParseException e) {
-
e.printStackTrace();
-
}
-
-
return null;
-
}
-
/**
-
* 将UTC时间转换为东八区时间
-
* @param UTCTime
-
* @return
-
*/
-
public static String getLocalTimeFromUTC(String UTCTime){
-
Date UTCDate;
-
SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
-
-
String localTimeStr = null ;
-
try {
-
UTCDate = format.parse(UTCTime);
-
format.setTimeZone(TimeZone.getTimeZone( "GMT-8")) ;
-
localTimeStr = format.format(UTCDate) ;
-
} catch (ParseException e) {
-
e.printStackTrace();
-
}
-
-
return localTimeStr ;
-
}
再来个更高级的,可以获得各个时区的时间
-
/**
-
* 获得任意时区的时间
-
*
-
* @param timeZoneOffset
-
* @return
-
*/
-
public static String getFormatedDateString(float timeZoneOffset) {
-
if (timeZoneOffset > 13 || timeZoneOffset < - 12) {
-
timeZoneOffset = 0;
-
}
-
int newTime = ( int) (timeZoneOffset * 60 * 60 * 1000);
-
TimeZone timeZone;
-
String[] ids = TimeZone.getAvailableIDs(newTime);
-
if (ids.length == 0) {
-
timeZone = TimeZone.getDefault();
-
} else {
-
timeZone = new SimpleTimeZone(newTime, ids[ 0]);
-
}
-
-
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
-
sdf.setTimeZone(timeZone);
-
return sdf.format( new Date());
-
}
二、
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); }
参考: