1

I am calling a rest web service that accepts Date. On client side, i have calling this service using JDK 8 OffsetDateTime Class.

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

Below is the code:

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());

Value of offsetDateTime that is coming with above code is 2018-07-01T05:30+05:30.

I am in IST time zone. Can someone help as to what needs to be done to change date to 2018-07-01T08:00:00.000+0000.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Raj Kumar
  • 73
  • 4
  • 1
    When you can use `OffsetDateTime`, avoid the old, out-dated and poorly designed `Calendar` class completely. java.time, the modern Java date and time API, offers all the functionality you need (and more). – Ole V.V. Aug 10 '18 at 06:10
  • 1
    FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Aug 10 '18 at 06:55
  • Your comments make it a bit unclear: Do you need an `OffsetDateTime` object, a `String` or both? – Ole V.V. Aug 10 '18 at 12:51

4 Answers4

2

tl;dr

If you want 8 AM on first day of July at UTC…

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

Avoid legacy date-time classes

Never use Calendar or Date classes. They were completely supplanted by the modern java.time classes such as OffsetDateTime. You are mixing the legacy classes with the modern, and that makes no sense.

java.time

Your Question is not clear about what are your inputs and what are your outputs versus your expectations.

If you goal is 8 AM on July 1 in UTC:

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

That string format complies with ISO 8061 standard. If your destination refuses that input and accepts only 2018-07-01T08:00:00.000+0000, then we must defining a formatting pattern.

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


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

i think the below code will work

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;
}
Test12345
  • 1,625
  • 1
  • 12
  • 21
  • Executing this code gave output : Fri Aug 10 06:09:43 IST 2018 . I need date in T08:00:00.000+0000 kind of format. – Raj Kumar Aug 10 '18 at 06:11
  • 1
    Also, i need date with OffsetDateTime class while the above code returns Date class. – Raj Kumar Aug 10 '18 at 06:13
  • 1
    FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Aug 10 '18 at 06:55
1

You can do it like so,

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

Update

If you need an instance of OffsetDateTime here it is.

offsetDateTime.atZoneSameInstant(ZoneId.of("Asia/Kolkata")).toOffsetDateTime();
Ravindra Ranwala
  • 20,744
  • 6
  • 45
  • 63
0

It’s not the answer you asked for, but it may be the answer you prefer in the end: Check once more whether the service you are calling accepts the format that you are already giving it. Both formats conform with ISO 8601, so it seems that the service accepts this standard format. If so, it should accept yours too.

In any case, use OffsetDateTime and the other classes from java.time exclusively and avoid the old and outdated Calendar and TimeZone classes. Basil Bourque’s answer shows the good solution.

Link: Wikipedia article: ISO 8601

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161