Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing date to remove timezone

I use the following SimpleDateFormat to parse a string,

 SimpleDateFormat ft= new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");
 String notimeZone = ft.format(startDateTime);
 Date date = ft.parse(notimeZone);

startDateTime is Date object with value in format "Thu Mar 06 12:27:55 IST 2014".

in notimeZone variable i get, Thu Mar 06 12:27:55 2014

I am expecting output in variable date as, Thu Mar 06 12:27:55 2014

But am getting same , Thu Mar 06 12:27:55 IST 2014.

How to remove the time zone from the date object.

Please help.

like image 364
user3164187 Avatar asked Mar 20 '23 08:03

user3164187


1 Answers

java.util.Date does not have a Timezone. It is not aware of TimeZone.

When you print, java picks up the default time zone.

Conceptually, you cannot have a date time without timezone. A date time has to be in one and only one zone. You may convert it to other zones, but it can never be without a zone.

If your business use case requires awareness of Time Zone, I would prefer to use Calendar class, or Joda Time. Avoid playing with Date class. If your business use cases do not require any time zone awareness, then go ahead with date. Assume all Date instances are in your default time zone.

Best thing is to use Calendar (or Joda Time). Calendar is a bit unintuitive but you will be able to get your job done. It is not without reason that Date class is mostly deprecated.

Initialize your Calendar with Timezone (check out available constructors). Use Dateformat to convert and show in UI in whatever time zone you wish to. I posted some code in your previous question. That might help you. I was stuck in a similar problem. I have dates in the DB in one time zone. To display them in another time zone on UI, you need to convert that datetime to another zone.

like image 133
RuntimeException Avatar answered Mar 29 '23 10:03

RuntimeException