Java获得指定时区时间

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

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

或者:
Calendar cal = Calendar.getInstance(Locale.CHINA);
写几个实例:
/**
* 获得东八区时间
*
* @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());
}

上面这些方法也是在网上看别的网友写的,如果大家有更好的方式,请不吝赐教


参考:


### Java中设置和使用东八区时区时间 #### 使用`java.time`包处理东八区时间 对于现代Java版本,推荐使用`java.time`包来处理时间和日期。此方法更加简洁且线程安全。 ```java import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class Main { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); ZonedDateTime zonedNow = now.atZone(ZoneId.of("Asia/Shanghai")); // 设置为东八区时间 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); System.out.println(zonedNow.format(formatter)); } } ``` 这段代码展示了如何通过指定`ZoneId.of("Asia/Shanghai")`将当前本地时间转换成中国标准时间(即东八区),并按照特定格式打印出来[^1]。 #### 利用`SimpleDateFormat`与`TimeZone`实现东八区时间显示 另一种方式适用于较旧版的Java环境,在这种情况下可以采用`SimpleDateFormat`配合`TimeZone`对象完成相同功能: ```java import java.text.SimpleDateFormat; import java.util.Date; import java.util.SimpleTimeZone; import java.util.TimeZone; public class Main { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); int timeOffset = 8 * 60 * 60 * 1000; // 计算东八区偏移量 String id = TimeZone.getAvailableIDs(timeOffset)[0]; // 获取对应ID TimeZone timeZone = new SimpleTimeZone(timeOffset, id); sdf.setTimeZone(timeZone); System.out.println(sdf.format(new Date())); } } ``` 这里创建了一个具有东八区特性的`SimpleTimeZone`实例,并将其应用到`SimpleDateFormat`上以便于格式化输出带有正确时区信息的时间字符串[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值