1

我正在使用 oracle MAF 开发移动应用程序。Oracle MAF 提供了它的日期组件,如果我选择一个日期,那么输出就像:2015-06-16T04:35:00.000Zfor selected date Jun 16, 2015 10:05 AM

我正在尝试使用 .ical(ICalendar 日期格式)将此格式转换为“印度标准时间”,这应该类似于20150613T100500所选日期Jun 16, 2015 10:05 AM。我正在使用下面的代码:

SimpleDateFormat isoFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
isoFormat.setTimeZone(TimeZone.getTimeZone("IST"));
String start_date_time = isoFormat.parse("20150616T043500000Z").toString();

但它返回日期时间为:

Tue Jun 16 04:35:00 GMT+5:30 2015

应该是这样的:

20150616T100500
4

3 回答 3

1

您需要将值从2015-06-16T04:35:00.000ZUTC 解析为java.util.Date

SimpleDateFormat from = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
from.setTimeZone(TimeZone.getTimeZone("UTC"));
Date start_date_time = from.parse("2015-06-16T04:35:00.000Z");

这给了我们一个java.util.DateTue Jun 16 14:35:00 EST 2015对我来说)。

然后,您需要在 IST 中对其进行格式化

SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
outFormat.setTimeZone(TimeZone.getTimeZone("IST"));
String formatted = outFormat.format(start_date_time);
System.out.println(formatted);

哪个输出20150616T100500

Java 8 时间 API

只是因为这是一个很好的练习...

    // No Time Zone
    String from = "2015-06-16T04:35:00.000Z";
    LocalDateTime ldt = LocalDateTime.parse(from, DateTimeFormatter.ISO_ZONED_DATE_TIME);
    
    // Convert it to UTC
    ZonedDateTime zdtUTC = ZonedDateTime.of(ldt, ZoneId.systemDefault()).withZoneSameInstant(ZoneId.of("UTC"));

    // Convert it to IST
    ZonedDateTime zdtITC = zdtUTC.withZoneSameInstant(ZoneId.of("Indian/Cocos"));
    String timestamp = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss").format(zdtITC);
    System.out.println(timestamp);

nb:如果我没有将值解析为LocalDateTime,则将其转换为UTC,我已经过了一个小时,但我愿意了解更好的方法

于 2015-06-16T04:54:13.880 回答
0

供应格式应为"yyyy-MM-dd'T'HH:mm:ss"

public static void main(String[] args) {
    SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    isoFormat.setTimeZone(TimeZone.getTimeZone("IST"));
    try {
        Date start_date_time = isoFormat.parse("2015-06-16T04:35:00.000Z");
        System.out.println(start_date_time);
        SimpleDateFormat output = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
        String formattedTime = output.format(start_date_time);
        System.out.println(formattedTime);
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

输出

Tue Jun 16 04:35:00 IST 2015
20150616T043500
于 2015-06-16T04:43:05.380 回答
0

对格式进行了一些补充,并在日期字符串中添加了正确的 TZ:

 SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz");
 isoFormat.setTimeZone(TimeZone.getTimeZone("IST"));
 String start_date_time = isoFormat.parse("2015-06-16T04:35:00.000CEST").toString();
于 2015-06-16T04:46:42.880 回答