【java】根据当前时区获取时间

地区国家 编号 缩写 时区
中国 86 CN Asia/Shanghai
香港 852 HK Asia/Hong_Kong
澳门 853 MO Asia/Macau
台湾 886 TW Asia/Taipei
新加坡 65 SG Asia/Singapore
泰国 66 TH Asia/Bangkok
印度 91 IN Asia/Calcutta
日本 81 JP Asia/Tokyo
韩国 82 KR Asia/Seoul
巴基斯坦 92 PK Asia/Karachi
美国 1 US America/Los_Angeles
英国 44 GB Europe/London
法国 33 FR Europe/Paris
德国 49 DE Europe/Berlin
印度尼西亚 62 ID Asia/Jakarta
马来西亚 60 MY Asia/Kuala_Lumpur
在这里插入图片描述

 /**
     * 获取指定时区的当前时间字符串
     * @param timezone +8:00
     * @return
     */
    public static String getDateString(String timezone){
        //设置时区,如:+8:00 或者取时区id(GMT)
        TimeZone time = TimeZone.getTimeZone(timezone);
        //格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 设置时区
        TimeZone.setDefault(time);
        // 获取实例
        Calendar calendar = Calendar.getInstance();
        //获取Date对象
        Date date = calendar.getTime();
        return format.format(date);
    }

扩展方法

    /**
     * 获取指定时区的当前时间字符串
     * @param timezone GMT
     * @return
     */
    public static String getDateString(String timezone){
        // 获取实例
        Calendar calendar = Calendar.getInstance();
        //获取Date对象
        Date date = calendar.getTime();
        return getDateString(date,timezone);
    }

    /**
     * 获取指定时区的时间字符串
     * @param date
     * @param timezone GMT
     */
    public static String getDateString(Date date, String timezone){
        //设置时区,如:+8:00 或者取时区id(GMT)
        TimeZone time = TimeZone.getTimeZone(timezone);
        //格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 设置时区
        TimeZone.setDefault(time);
        return format.format(date);
    }

    /**
     * 将字符串转为date对象
     * @param string
     * @return
     */
    public static Date getDateFromString(String string)throws ParseException {
        //定义一个格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //转换
        Date date = simpleDateFormat.parse(string);
        return date;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用Java 8中的java.time包提供的类和方法来获取特定时区时间。 以下是一个示例代码: ```java // 获取指定时区当前时间 ZoneId zoneId = ZoneId.of("Asia/Shanghai"); LocalDateTime localDateTime = LocalDateTime.now(zoneId); // 格式化时间 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = localDateTime.format(formatter); // 输出结果 System.out.println(formattedDateTime); ``` 在上面的代码中,我们首先使用`ZoneId.of()`方法获取指定时区(例如Asia/Shanghai)的`ZoneId`对象,然后使用`LocalDateTime.now()`方法获取当前时间。由于这里使用了指定的时区,因此获取时间也是该时区的本地时间。 接着,我们使用`DateTimeFormatter.ofPattern()`方法定义时间格式,并使用`LocalDateTime.format()`方法将时间格式化为字符串。最后,我们输出格式化后的时间字符串。 需要注意的是,`LocalDateTime`表示的是本地日期时间,不包含时区信息。如果需要处理时区,可以使用`ZonedDateTime`类。例如: ```java // 获取指定时区当前时间 ZoneId zoneId = ZoneId.of("Asia/Shanghai"); ZonedDateTime zonedDateTime = ZonedDateTime.now(zoneId); // 格式化时间 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z"); String formattedDateTime = zonedDateTime.format(formatter); // 输出结果 System.out.println(formattedDateTime); ``` 在上面的代码中,我们使用`ZonedDateTime.now()`方法获取指定时区当前时间,并使用`DateTimeFormatter`格式化时间。最后,我们输出格式化后的时间字符串。需要注意的是,在格式化字符串中添加了时区信息(使用“z”表示)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

assassin丶J

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值