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



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

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

按照这个逻辑,我得到的日期是2021年6月8日星期二00:00:00,但我想要此格式的输出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)

您可以将输入的日期时间解析为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]

java.time

java.util日期-时间API及其格式API、SimpleDateFormat已过时且存在错误。建议完全停止使用它们,并切换到现代日期时间API*

使用现代日期时间APIjava.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

在线演示

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


*出于任何原因,如果您必须坚持使用Java 6或Java 7,您可以使用ThreeTen BackportJava.time的大部分功能向后移植到Java 6&7.如果您正在为Android项目工作,并且您的Android API级别仍然不符合Java-8,请检查通过desugaring和如何在Android项目中使用ThreeTenABP提供的Java 8+API

最新更新