Java 根据时间与地区查出当地时区

    public static String timeZone(Date date,ZoneId zoneId) throws ParseException {
       //先得出世界时间
        LocalDateTime UTCTime = LocalDateTime.ofInstant(date.toInstant(), TimeZone.getTimeZone("UTC").toZoneId());
        LocalDateTime GBTIme = LocalDateTime.ofInstant(date.toInstant(), zoneId);
        System.out.println(UTCTime);
        System.out.println(GBTIme);
        
        Long utc = UTCTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        Long gb = GBTIme.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        
        System.out.println(date.getTime() - gb);
        System.out.println(date.getTime() - utc);
        
        System.out.println(utc);
        System.out.println(gb);

        //计算时间差
        if (UTCTime.compareTo(GBTIme) > 0) {
            Long gmt = (utc - gb) / 1000 / 60 / 60;
            return "GMT-" + gmt.toString() + ":00";
        } else {
            Long gmt = (gb - utc ) / 1000 / 60 / 60;
            return "GMT+" + gmt.toString() + ":00";
        }
    }
    
    
    public static void main(String[] args) {
        try {
          
     		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    		dateFormat.setTimeZone(TimeZone.getTimeZone("CN"));
		    Date date = dateFormat.parse("2023-04-02 01:00:00");

            System.out.println(timeZone(date, TimeZone.getTimeZone("Europe/London").toZoneId()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }


 //这个办法更好
    public static void main(String[] args) {
        // 设置默认时区为美国纽约
        TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"));

        // 获取当前默认时区
        TimeZone timeZone = TimeZone.getDefault();

        // 输出时区信息
        System.out.println("地区: " + timeZone.getDisplayName());
        System.out.println("时区ID: " + timeZone.getID());
        System.out.println("时区偏移量: " + timeZone.getRawOffset() / (60 * 60 * 1000) + " 小时");
    }

给定一个时间,求出当地时区

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,我们可以使用`java.time.LocalDateTime`和`java.time.ZoneId`类来处理时区格式化时间。下面是一个示例代码,演示如何将本地时间转换为指定时区时间,并进行格式化输出: ```java import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; public class Example { public static void main(String[] args) { // 获取本地时间 LocalDateTime localTime = LocalDateTime.now(); // 创建时区对象 ZoneId zoneId = ZoneId.of("Asia/Shanghai"); // 将本地时间转换为指定时区时间 LocalDateTime tzTime = localTime.atZone(ZoneId.systemDefault()) .withZoneSameInstant(zoneId).toLocalDateTime(); // 格式化输出时间 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z"); String formattedTime = tzTime.format(formatter); System.out.println(formattedTime); } } ``` 在上面的代码中,我们首先获取了本地时间,然后创建了一个指定时区时区对象。接着,通过`atZone()`方法将本地时间转换为默认时区时间,再通过`withZoneSameInstant()`方法将时区改为指定时区,最后通过`toLocalDateTime()`方法将时间转换为本地时间。最后,使用`DateTimeFormatter`类将时间格式化为字符串输出。 如果你希望将指定时区时间转换为本地时间,可以将上面的代码中的`zoneId`和`ZoneId.systemDefault()`交换位置。例如: ```java import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; public class Example { public static void main(String[] args) { // 获取指定时区时间 ZoneId zoneId = ZoneId.of("Asia/Shanghai"); LocalDateTime tzTime = LocalDateTime.now(zoneId); // 将指定时区时间转换为本地时间 LocalDateTime localTime = tzTime.atZone(zoneId) .withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime(); // 格式化输出时间 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z"); String formattedTime = localTime.format(formatter); System.out.println(formattedTime); } } ``` 在上面的代码中,我们首先获取了指定时区时间,然后创建了一个本地时区对象。接着,通过`atZone()`方法将时间转换为指定时区时间,再通过`withZoneSameInstant()`方法将时区改为本地时区,最后通过`toLocalDateTime()`方法将时间转换为本地时间。最后,使用`DateTimeFormatter`类将时间格式化为字符串输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值