0

I have an android app that receives a string in this format:"MM-dd-yyyy HH:mm:ss" from my server. I want to convert this string to a Date object with UTC as timezone since the time in the string is UTC. I've already checked several similar questions but didn't find my answer

Here is what I'm using currently:

SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
            Date date = new Date();
            try {
                date = format.parse(itemContent [3]);
                entity.setValidTill(date);
            }catch (Exception e){

            }

But what it does when I print that date with Log is show it as:

Sun Aug 27 15:00:00 GMT+04:00 2017

I want it to be:

Sun Aug 27 15:00:00 GMT 00:00 2017

So here is the main question how to get DateTime for UTC using a string with format as above?

Edit:

Just put it in a better context. I'm trying to get users to see the difference between current datetime & the that datetime saved in server. So my solution was to get gmt time for users & compare with the server time(which is gmt) so everyone see same difference regardless of their timezone. With C# you can get DateTime.UtcNow while with java I couldn't find an alternative

Cobra47
  • 709
  • 1
  • 10
  • 17
  • [`DateFormat.setTimeZone(TimeZone)`](https://docs.oracle.com/javase/8/docs/api/java/text/DateFormat.html#setTimeZone-java.util.TimeZone-) (before you parse). – Elliott Frisch Aug 27 '17 at 14:24
  • Is your issue with printing the date or persisting it? – alirabiee Aug 27 '17 at 14:37
  • Please check my edit. I've explained a bit what I'm aiming with to do with this – Cobra47 Aug 27 '17 at 14:53
  • Do you know what time zone is intended but not labeled in that incoming string? Is it UTC? – Basil Bourque Aug 27 '17 at 17:03
  • These issues have been covered *many* times already on Stack Overflow. Search for class names LocalDateTime, Instant, OffsetDateTime, ZonedDateTime, ZoneOffset.UTC, ZoneId, and the project ThreeTenABP for Android, and the ISO 8601 standard. – Basil Bourque Aug 27 '17 at 17:21

2 Answers2

1

Briefly, as your Question is really a duplicate of many others…

You are using troublesome old date-time classes that are now legacy, supplanted by the java.time classes.

Parse your input string as a LocalDateTime as it lacks any indicator of offset-from-UTC or time zone.

Define a formatter to parse your input string.

String input = "08-27-2017 15:00:00" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM-dd-uuuu HH:mm:ss" ) ;
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;

ldt.toString(): 2017-08-27T15:00

A LocalDateTime is not a moment on the timeline, only a rough idea about a range of possible moments. Has no meaning without the context of an offset (or time zone).

If you are certain that input was intended for UTC, assign the constant ZoneOffset.UTC for a OffsetDateTime.

OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;

odt.toString(): 2017-08-27T15:00Z

To calculate a delta between that moment and the current moment, use the Period class for coarser granularity in your span of time, or Duration for finer granularity. Both classes generate strings in standard ISO 8601 format of PnYnMnDTnHnMnS.

OffsetDateTime now = OffsetDateTime.now( ZoneOffset.UTC ) ;
Duration d = Duration.between( odt , now ) ; 

now.toString(): 2017-08-27T21:16:56.396Z

d.toString(): PT6H16M56.396S

See this code run live at IdeOne.com.

In the standard strings seen above, the Z is short for Zulu and means UTC.


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?

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

just add this code under the first line of your code:

format.setTimeZone(TimeZone.getTimeZone("UTC"));
Javdroider
  • 136
  • 8
  • The datetime coming from server is already in UTC. So setting timezone will add 3 hours to time in my timezone – Cobra47 Aug 27 '17 at 14:59