根据groovy中存储的时区将Java日期转换为特定时区

问题描述 投票:0回答:3

我在数据库中以字符串格式存储时区,并且正在渲染一个表,其中包含开始日期和结束日期,下面是我的实现

假设我的时区是亚洲/科伦坡 我正在将日期 2021-06-08T00:00:00 转换为亚洲/科伦坡

时区

按照这种逻辑,我得到的日期格式为 Tue Jun 08 00:00:00 IST 2021,但是 我想要这种格式的输出2021-06-08T00:00:00(具有特定时区)

我应该在当前的实现中更改哪些内容才能获得所需的输出?

而且每当我以日期类型返回日期时,它都会发生变化

以字符串形式返回日期或以日期形式返回日期哪个更好

        List<TimeZone> timeZoneList = TimeZone.findAll()
        String zone = timeZoneList ? timeZoneList[0]?.currentTimeZone : null
        String zoneId = zone ? TimeZone.getTimeZone(zone).ID : TimeZone.getDefault().ID

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateToConvert);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        System.out.println(sdf.format(calendar.getTime()));

        sdf.setTimeZone(TimeZone.getTimeZone(zoneId));
        String convertedDate = sdf.format(calendar.getTime())
        println("converted date in string format ----->>>>>>>>>>> " + convertedDate)
java grails groovy java-8
3个回答
2
投票

您可以将输入的日期时间解析为LocalDateTime并转换为ZoneDateTime

 String inputDate = "2021-06-08T00:00:00";

 ZonedDateTime zonedDateTime = LocalDateTime.parse(inputDate).atZone(ZoneId.of("Asia/Colombo"));

 System.out.println(zonedDateTime); //2021-06-08T00:00+05:30[Asia/Colombo]

1
投票

java.time

java.util
日期时间 API 及其格式化 API
SimpleDateFormat
已过时且容易出错。建议完全停止使用它们并切换到现代日期时间 API*

使用现代日期时间 API

java.time
进行演示:

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String strDateTime = "2021-06-08T00:00:00";
        ZonedDateTime zdtUtc = LocalDateTime.parse(strDateTime).atZone(ZoneId.of("Etc/UTC"));
        ZonedDateTime zdtColombo = zdtUtc.withZoneSameInstant(ZoneId.of("Asia/Colombo"));
        System.out.println(zdtColombo);

        // Output in a custom format
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ENGLISH);
        String formatted = dtf.format(zdtColombo);
        System.out.println(formatted);
    }
}

输出:

2021-06-08T05:30+05:30[Asia/Colombo]
2021-06-08 05:30:00

在线演示

如果您的列类型是

DATE
,请选中 此答案

如果您的列类型是

TIME WITH TIMEZONE
,请选中 此答案

如何将
LocalDate
转换为
ZonedDateTime

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // A sample LocalDate (to be retrieved with JDBC)
        LocalDate date = LocalDate.of(2021, 6, 8);
        
        ZonedDateTime zdtUtc = date.atStartOfDay(ZoneId.of("Etc/UTC"));
        ZonedDateTime zdtColombo = zdtUtc.withZoneSameInstant(ZoneId.of("Asia/Colombo"));
        System.out.println(zdtUtc);
        System.out.println(zdtColombo);

        // Output in a custom format
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ENGLISH);
        System.out.println(dtf.format(zdtUtc));
        System.out.println(dtf.format(zdtColombo));
    }
}

输出:

2021-06-08T00:00Z[Etc/UTC]
2021-06-08T05:30+05:30[Asia/Colombo]
2021-06-08 00:00:00
2021-06-08 05:30:00

在线演示

Trail:日期时间了解有关现代日期时间 API 的更多信息。


* 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,则可以使用 ThreeTen-Backport,它将大部分 java.time 功能向后移植到 Java 6 和 7。正在处理 Android 项目,并且您的 Android API 级别仍然不符合 Java-8,请检查 通过脱糖可用的 Java 8+ API 如何在 Android 项目中使用 ThreeTenABP


0
投票

假设您的时区是“Aisa/Kolkata”,并且您希望该时区的当前时间采用 AM/PM 格式。 一种方法是:

字符串时区=“亚洲/加尔各答” 字符串 DATEPICKER_FORMAT_AM_PM_HOURS = "dd-MMM-yyyy hh:mm a"

DateTimeFormatter 格式化程序 = DateTimeFormat.forPattern(DATEPICKER_FORMAT_AM_PM_HOURS).withZone(DateTimeZone.forID(timeZone)) new DateTime(new Date()).toString(formatter)

假设当前 UTC 时间为中午 12:00,日期为 2024 年 5 月 22 日。 那么输出将是:22-May-2024 05:30 PM

您可以使用任何您想要的日期格式以及您想要转换日期的时区。

© www.soinside.com 2019 - 2024. All rights reserved.