-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

9
  • see this post stackoverflow.com/questions/17677546/…. Commented Sep 13, 2020 at 9: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" Commented Sep 13, 2020 at 9:12
  • post your input and your desired output Commented Sep 13, 2020 at 9:15
  • input Date - Tue Sep 01 14:14:25 GMT+05:30 2020 Output Date - 2020-09-01T08:44:25.654Z @UmairMubeen Commented Sep 13, 2020 at 9: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. Commented Sep 13, 2020 at 9:34

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.