将日期转换为08:00:00.000 + 0000

时间:2018-08-10 05:58:50

标签: java date java-8 java-time

我正在呼叫接受Date的Rest Web服务。在客户端,我已经使用JDK 8 OffsetDateTime类调用此服务。

Value that is going from my client side : 2018-07-01T05:30+05:30
Value that is accepted by Service : 2018-07-01T08:00:00.000+0000

下面是代码:

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(ZoneId.of("UTC")));
cal.set(2018, 05, 31);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.HOUR_OF_DAY, 0);
OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(cal.getTime().toInstant(), ZoneId.systemDefault());

上面的代码附带的offsetDateTime的值为2018-07-01T05:30 + 05:30。

我在IST时区。 有人可以帮忙将日期更改为2018-07-01T08:00:00.000 + 0000时需要做什么。

4 个答案:

答案 0 :(得分:2)

tl; dr

如果您想在世界标准时间7月1日上午8点…

OffsetDateTime.of( 
    2018 , 7 , 1 ,     // Date (year, month 1-12 is Jan-Dec, day-of-month)
    8 , 0 , 0 , 0 ,    // Time (hour, minute, second, nano)
    ZoneOffset.UTC     // Offset-from-UTC (0 = UTC)
)                      // Returns a `OffsetDateTime` object.
.format(               // Generates a `String` object with text representing the value of the `OffsetDateTime` object.
    DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH:mm:ss.SSSZ" , Locale.US )
)                      // Returns a `String` object.
  

2018-07-01T08:00:00.000 + 0000

避免使用旧的日期时间类

请勿使用CalendarDate类。它们被现代的 java.time 类(例如OffsetDateTime)完全取代。您正在将遗留类与现代类混合在一起,这没有任何意义。

java.time

您的问题不清楚您的投入是什么,什么是产出与期望的对比。

如果您的目标是在UTC于7月1日上午8点:

LocalDate ld = LocalDate.of( 2018 , Month.JULY , 1 ) ;
LocalTime lt = LocalTime.of( 8 , 0 ) ;
OffsetDateTime odt = OffsetDateTime.of( ld , lt , ZoneOffset.UTC ) ;
  

odt.toString():2018-07-01T08:00Z

该字符串格式符合ISO 8061标准。如果您的目的地拒绝该输入并仅接受2018-07-01T08:00:00.000+0000,那么我们必须定义一种格式设置模式。

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH:mm:ss.SSSZ" , Locale.US );
String output = odt.format( f );
  

2018-07-01T08:00:00.000 + 0000


关于 java.time

java.time框架已内置在Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.DateCalendarSimpleDateFormat

目前位于Joda-Timemaintenance mode项目建议迁移到java.time类。

要了解更多信息,请参见Oracle Tutorial。并在Stack Overflow中搜索许多示例和说明。规格为JSR 310

您可以直接与数据库交换 java.time 对象。使用符合JDBC driver或更高版本的JDBC 4.2。不需要字符串,不需要java.sql.*类。

在哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展了java.time。该项目为将来可能在java.time中添加内容提供了一个试验场。您可能会在这里找到一些有用的类,例如IntervalYearWeekYearQuartermore

答案 1 :(得分:1)

我认为下面的代码可以工作

public static Date ConvertToGMT() {
    Date date = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date utc = new Date(dateFormat.format(date));
    return utc;
}

答案 2 :(得分:1)

您可以这样做

public static List<participant> participantList(int _id) => bL.asset.participants.eventrecord.List(_id, ref _em);

更新

如果您需要这里的 public static void Report(List<object> _args, ref report item, ref DataTable dtReport) { object[] _params = new object[_args.Count]; MethodInfo _method = typeof(reportmethods).GetMethod(item.methodname); Type _t = _method.ReturnType; if (item.reportvariables != null) { if (item.reportvariables.Count > 0) { int x = 0; foreach (reportvariable current in item.reportvariables) { _params[x] = _args[x]; x++; } object result = _method.Invoke(null, _params); //List<_t.GetType()> _tls = new List<_t.GetType()>(); if (result is IList && result.GetType().IsGenericType) { } } } else { object result = _method.Invoke(null, null); } } 实例。

   offsetDateTime.atZoneSameInstant(ZoneId.of("Asia/Kolkata"))

答案 3 :(得分:0)

这不是您要的答案,但最终可能是您更喜欢的答案:再次检查您正在调用的服务是否接受您已经提供的格式。两种格式均符合ISO 8601,因此该服务似乎接受此标准格式。如果是这样,它也应该接受您的。

在任何情况下,都应单独使用OffsetDateTime和java.time中的其他类,并避免使用旧的和过时的CalendarTimeZone类。罗勒·布尔克(Basil Bourque)的答案显示了很好的解决方案。

链接: Wikipedia article: ISO 8601

相关问题