The Artima Developer Community
Sponsored Link

Java Answers Forum
Date parsing

3 replies on 1 page. Most recent reply: Mar 5, 2004 9:38 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Date parsing Posted: Mar 3, 2004 2:53 AM
Reply to this message Reply
Advertisement
I've just written a wee method that parses a date string in ISO format ("YYYY-MM-DD HH:MM:SS.S") into a local format ("DDMONYYYY"), e.g. "2004-03-04 10:45:17.2" to "04MAR2004".

Two questions (one rhetorical):
1) Why doesn't everyone accept ISO format date strings?
2) Do the java libraries already contain a suitable
parsing/conversion method for date strings?

Vince.


Jeroen Wenting

Posts: 88
Nickname: jwenting
Registered: Mar, 2004

Re: Date parsing Posted: Mar 3, 2004 3:14 AM
Reply to this message Reply
> I've just written a wee method that parses a date string
> in ISO format ("YYYY-MM-DD HH:MM:SS.S") into a local
> format ("DDMONYYYY"), e.g. "2004-03-04 10:45:17.2" to
> "04MAR2004".
>
> Two questions (one rhetorical):
> 1) Why doesn't everyone accept ISO format date strings?
Legacy, and because of the many regional differences.
Also, many endusers aren't aware of the fact they should use nice ISO standardised date formats and will type in any garbage and expect the software to understand it.

To hit you for your own program, why do you convert into a local format and not ISO standard :)

> 2) Do the java libraries already contain a suitable
> parsing/conversion method for date strings?
>
Yes. Take a look at SimpleDateFormat.

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Date parsing Posted: Mar 3, 2004 4:47 AM
Reply to this message Reply
> > Two questions (one rhetorical):
> > 1) Why doesn't everyone accept ISO format date
> strings?
> Legacy, and because of the many regional differences.
> Also, many endusers aren't aware of the fact they should
> use nice ISO standardised date formats and will type in
> any garbage and expect the software to understand it.

Ah I agree entirely, except that this is going to be an automated computer-to-computer process (no UI) to a foreign government's computer system via a brand new process specifically designed for internation use

> To hit you for your own program, why do you convert into a local format and not ISO standard :)

That's how 'they' want to receive it.

> > 2) Do the java libraries already contain a suitable
> > parsing/conversion method for date strings?
> >
> Yes. Take a look at SimpleDateFormat.

Excellent.

Thanks,
Vince.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Date parsing Posted: Mar 5, 2004 9:38 AM
Reply to this message Reply
Suggest you check out Internationalization.
http://java.sun.com/j2se/1.4.2/docs/guide/intl/index.html

There is a whole demo section on Date and time formatting at:
http://java.sun.com/j2se/1.4.2/docs/guide/intl/demos/DateTimeFormat/1.1/example1.html

as well an numbers, currency, etc.
 
    /** Returns the difference in milliseconds between two dates formatted as:
     *    6/16/2003 06:30:35
     *    6/16/2003 06:31:37
     */
    public long getDifference(String first, String second){
        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
        Date firstDate = format.parse(first, new ParsePosition(0));
        Date secondDate = format.parse(second, new ParsePosition(0));
        return Math.abs(firstDate.getTime() - secondDate.getTime());
        
    }
    
    /** Returns a java.util.Date from a string formatted as:
     *    6/16/2003 06:30:35
     */
    public Date getDate(String dateString){
        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
        return format.parse(dateString, new ParsePosition(0));
    }

Flat View: This topic has 3 replies on 1 page
Topic: ejecting CD ROM Drive thru java code. Previous Topic   Next Topic Topic: jtapi implementation

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use