• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

parse date string

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can we parse a string date and return a Date object.

When I do this using DateFormat.Parse() , it parses date string but does not parse timezone in the string.

For example, if we parse December 23, 2010 AM GMT and then print returned Date using System.out.println(Date date), it prints December 23, 2010 AM IST.

I want GMT instead of IST.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at JavaDatesFaq
 
Ashu Bharadwaj
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohamed Sanaulla wrote:Have a look at JavaDatesFaq



Please check the following code, this displays IST in time. I want Date object to be returned by the parsing.


import java.text.*;
import java.util.*;

public class Converter
{
public static Date calculate(Date locdate){
final int msInMin = 60000;
final int minInHr = 60;
int Hours, Minutes;
DateFormat dateFormat = DateFormat.getDateTimeInstance(
DateFormat.MEDIUM, DateFormat.MEDIUM);
TimeZone zone = dateFormat.getTimeZone(); /*gets local timezone */

Minutes =zone.getOffset( locdate.getTime() ) / msInMin;
Hours = Minutes / minInHr;
zone = zone.getTimeZone( "GMT Time" +(Hours>=0?"+":"")+Hours+":"+ Minutes);
dateFormat.setTimeZone( zone );
String str = dateFormat.format( locdate );



System.out.println(""+str);


SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a");



Date date = new Date();
try { date = formatter.parse(str);}
catch (ParseException e) {System.out.println( "exp" +e.getMessage());}


return date;


}



public static void main( String[] args ) {
Date done = new Date ();

DateFormat dateFormat = DateFormat.getDateTimeInstance(
DateFormat.LONG, DateFormat.LONG );


done = calculate(done);

String tk=dateFormat.format(done);
System.out.println(""+tk);
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags for posting your source code. The code has lot of date calculations, which are not clear for the reader.
But I would suggest you should be using the time zone information for the format being passed to SimpleDateFormat.

MMM dd, yyyy hh:mm:ss a to MMM dd, yyyy hh:mm:ss a Z
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first thing to understand is that class Date does not know anything about timezones. So, if you parse a string that contains a timezone indication into a Date object, then that Date object will not remember what the timezone was in the string. A Date object is nothing more than an absolute point in time, independent of the timezone.

When you want to format a date so that the string will contain a specific timezone, you must set the timezone on the DateFormat object before formatting it. For example:

 
Ashu Bharadwaj
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:The first thing to understand is that class Date does not know anything about timezones. So, if you parse a string that contains a timezone indication into a Date object, then that Date object will not remember what the timezone was in the string. A Date object is nothing more than an absolute point in time, independent of the timezone.

When you want to format a date so that the string will contain a specific timezone, you must set the timezone on the DateFormat object before formatting it. For example:



Thank you for your insight.
There is an issue here, when we set the timezone using the line: dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
we are not displaying the parsed date string. This line of code adjusts time according to the offset and displays in GMT. In my case reduces 5.30 hr.

I want the same string, which was parsed to be displayed. But should show GMT instead of IST.
Thanks.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ashu Bharadwaj wrote:... we are not displaying the parsed date string. This line of code adjusts time according to the offset and displays in GMT. In my case reduces 5.30 hr.


Ofcourse 5:30 is subtracted, because when it is 10:00 o'clock in Great Britain (GMT timezone), it is 15:30 in India (IST timezone) - there is a time difference of 5 hours and 30 minutes. You would have to subtract 5 hours and 30 minutes from the Date object, which you can do with a Calendar object - it contains methods to do calculations with dates and times.

But why exactly do you want to do that? Can you explain what your exact goal is with your code, so that we can help you find a good solution?
 
Ashu Bharadwaj
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:

Ashu Bharadwaj wrote:... we are not displaying the parsed date string. This line of code adjusts time according to the offset and displays in GMT. In my case reduces 5.30 hr.


Ofcourse 5:30 is subtracted, because when it is 10:00 o'clock in Great Britain (GMT timezone), it is 15:30 in India (IST timezone) - there is a time difference of 5 hours and 30 minutes. You would have to subtract 5 hours and 30 minutes from the Date object, which you can do with a Calendar object - it contains methods to do calculations with dates and times.

But why exactly do you want to do that? Can you explain what your exact goal is with your code, so that we can help you find a good solution?



Hi Jesper,

What I want is that to create a class which will accept Date object in IST and return Date object in GMT after calculation. The returned Date object should display GMT when we display using System.out.println(Date object);

Thanks.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, but do you really want to take a date and time, for example 26 Dec 2010, 16:00 IST and do something with it, and return 26 Dec 2010, 16:00 GMT? Those two timestamps do not indicate the same moment; 16:00 IST is 5 hours and 30 minutes earlier than 16:00 GMT.

Ashu Bharadwaj wrote:The returned Date object should display GMT when we display using System.out.println(Date object);


That is not possible (unless you set the timezone of your computer to GMT in the operating system settings). The Date object does not know about timezones, so when you print it out directly with System.out.println(...) it doesn't know that you want it printed with GMT.

If you want to print out a date with a specific timezone, for example GMT, then you'd have to format the date with a DateFormat object, and set the timezone on the DateFormat object:

*edit* Oh, I see that I already explained that above...

It is not possible to store timezone information in a Date object. You cannot "return a Date object in GMT" because Date doesn't know about timezones.
 
Water! People swim in water! Even tiny ads swim in water:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic