Convert UTC String to UTC Date [Android - Java] [duplicate]

僤鯓⒐⒋嵵緔 提交于 2021-02-17 07:04:14

问题


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


回答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.



来源:https://stackoverflow.com/questions/63868966/convert-utc-string-to-utc-date-android-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!