-1

I want to convert UTC time to Date. It should return Date as

Expected result is : 2020-09-01T08:44:25.654Z

output is -> "Tue Sep 01 14:14:25 GMT+05:30 2020".

I am getting UTC but it is in String I want the same format in Date datatype.

I want it as Date with the above UTC format.

Does anyone know how to get it?

public static Date convertToUtcTime(Date date) throws ParseException {
     SimpleDateFormat sdf = new SimpleDateFormat(DateFormats.yyyyMMddThhmmssZ);  //"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"
     sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

     return sdf.parse(sdf.format(date));
}

Also i dont want to use toInstant method

[UPDATED]

Date strFromDate;
try {
     strFromDate = AppUtils.convertToUtcTime(myCalendar.getTime());
} catch (ParseException e) {
     e.printStackTrace();
}

then passing strFromDate to api request I am not getting expected result from strFromDate

  • see this post https://stackoverflow.com/questions/17677546/convert-utc-into-local-time-on-android#:~:text=String%20dateStr%20%3D%20%22Jul%2016%2C,%22))%3B%20Date%20date%20%3D%20df. – Umair Mubeen Sep 13 '20 at 09:08
  • @UmairMubeen i want the result as UTC format but in Date datatype. I want to send 2020-09-01T08:44:25.654Z this as Date in api request but it's going as "Tue Sep 01 14:14:25 GMT+05:30 2020" – Shruti Suryawanshi Sep 13 '20 at 09:12
  • post your input and your desired output – Umair Mubeen Sep 13 '20 at 09:15
  • input Date - Tue Sep 01 14:14:25 GMT+05:30 2020 Output Date - 2020-09-01T08:44:25.654Z @UmairMubeen – Shruti Suryawanshi Sep 13 '20 at 09:33
  • you are viewing the date on returned object either using System.out.println or in ide, this view is showing you toString version of date, if you need to display formatted date, you should return String from your method, which is formatted already to your needs. – Soni Sep 13 '20 at 09:34
  • @soniKumari i am getting the desired output in string format but i want to pass the output in date format to api. My current request -> Tue Sep 01 14:14:25 GMT+05:30 2020 this is going in Date format I want to send -> 2020-09-01T08:44:25.654Z in Date format – Shruti Suryawanshi Sep 13 '20 at 09:41
  • @ShrutiSuryawanshi your code is correct and seems to work! you must be wrong somewhere else. show us how you are using returned date object. – Omid.N Sep 13 '20 at 09:48
  • @Omid.N updated the post. please check – Shruti Suryawanshi Sep 13 '20 at 09:55
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project, in order to use java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Sep 13 '20 at 21:02

1 Answers1

1

Who is your favourite actor? Let's say it's Amitabh Bachchan. No matter what role he plays, he still remains himself (and that is why directors/producers go to him with next offer ). His acting decorates his look as per the role. What directors get using his acting skill is a decorated character representing him as per the specified role. And, then you say Amitabh Bachchan as an angry young man OR Amitabh Bachchan as a coolie etc.)

Similarly, no matter what kind of look you ask java.util.Date to take (using SimpleDateFormat), it always represents the no. of milliseconds from the epoch of 1970-01-01T00:00:00Z. It does not have any time-zone or zone-offset information. From this milliseconds, Java calculates the date and time based on the time-zone of your JVM and then combines the obtained date and time with the time-zone of your JVM and finally returns the string when its toString() method is called. I believe you already know that when you print an object using System.out.println, the string returned by the object's toString() method gets printed. Check this for more details of the toString() implementation of java.util.Date. A SimpeDateFormat changes the look of java.util.Date as per the pattern. What you get using a SimpeDateFormat is a decorated string representing the Date object as per the specified pattern. And, then you say Date as a string of EEE MMMM dd, yyyy HH:mm pattern OR Date as a string of yyyy-MM-dd HH:mm:ss pattern etc.

The summary is: You can not change Date object by specifying different patterns. If you print it, you will always get the string returned by its toString() function. If you want a different string, you can do so by using SimpleDateFormat.

A recommendation:

I recommend you switch from the outdated and error-prone java.util date-time API and SimpleDateFormat to the modern java.time date-time API and the corresponding formatting API (package, java.time.format). Learn more about the modern date-time API from Trail: Date Time.

If your Android API level is still not compliant with Java8, check How to use ThreeTenABP in Android Project and Java 8+ APIs available through desugaring.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • No, what about this line :`I am not getting expected result from strFromDate` ..... I have been trying to do it after seeing this question, but his code does not work it doesn't change the time zone of date obj. He has written that line because his English is bad (like me). Try it! – Omid.N Sep 13 '20 at 13:43
  • @Omid.N - OP's code compiles successfully. Please try `String strDate = "2020-09-01T08:44:25.654Z"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); Date date = sdf.parse(strDate); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); System.out.println(sdf.parse(sdf.format(date)));` – Arvind Kumar Avinash Sep 13 '20 at 13:49