aspose file tools*
The moose likes Java in General and the fly likes parse date string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "parse date string" Watch "parse date string" New topic
Author

parse date string

Ashu Bharadwaj
Greenhorn

Joined: Nov 16, 2010
Posts: 20
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.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Have a look at JavaDatesFaq


Mohamed Sanaulla | My Blog
Ashu Bharadwaj
Greenhorn

Joined: Nov 16, 2010
Posts: 20
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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ashu Bharadwaj
Greenhorn

Joined: Nov 16, 2010
Posts: 20
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

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

Joined: Nov 16, 2010
Posts: 20
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

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.
 
 
subject: parse date string