How to Parse Date from GMT TimeZone to IST TimeZone and Vice Versa in android

Date does not have any time zone. It is just a holder of the number of milliseconds since January 1, 1970, 00:00:00 GMT. Take the same DateFormat that you used for parsing, set IST timezone and format your date as in the following example DateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd’T’HH:mm:ss.SSSXXX”); Date date = sdf.parse(“2013-01-09T19:32:49.103+05:30”); sdf.setTimeZone(TimeZone.getTimeZone(“IST”)); System.out.println(sdf.format(date)); … Read more

Why can’t this SimpleDateFormat parse this date string?

First, three-char months are to be represented by MMM. Second, one-two digit hours are to be represented by h. Third, Mar seems to be English, you’ll need to supply a Locale.ENGLISH, else it won’t work properly in machines with a different default locale. The following works: SimpleDateFormat sdf = new SimpleDateFormat(“MMM dd yyyy h:mm:ss:SSSa”, Locale.ENGLISH); … Read more

Java: Date parsing, why do I get an error

Date date = new Date(); DateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd’T’HH:mm:ss.SSSZ”);//2018-02-05T18:00:51.001+0000 String text = dateFormat.format(date); try { Date test = dateFormat.parse(text); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } worked for me. With “SSSZ” instead of “SZ” at the end of the pattern.

SimpleDateFormat.parse() ignores the number of characters in pattern

There are serious issues with SimpleDateFormat. The default lenient setting can produce garbage answers, and I cannot think of a case where lenient has any benefit. The lenient setting is not a reliable approach to produce reasonable interpretations of human entered date variations. This should never have been the default setting. Use DateTimeFormatter instead if … Read more

How to convert HH:mm:ss.SSS to milliseconds?

You can use SimpleDateFormat to do it. You just have to know 2 things. All dates are internally represented in UTC .getTime() returns the number of milliseconds since 1970-01-01 00:00:00 UTC. package se.wederbrand.milliseconds; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; public class Main { public static void main(String[] args) throws Exception { SimpleDateFormat sdf = new … Read more