Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

Date parsing without separator

Thread view: 
denzel - 02 Nov 2006 15:27 GMT
Hello,

is there a way to parse a formatted date like '01Feb06' using
SimpleDateFormat?

"ddMMMyy" -> no
"dd''MMM''yy" -> no
???

TIA
Robert Klemme - 02 Nov 2006 15:54 GMT
> Hello,
>
[quoted text clipped - 4 lines]
> "dd''MMM''yy" -> no
> ???

If you do not find a way with the SDF you could use a regular expression
and use Calendar to set individual fields.

Regards

    robert
Jeffrey Schwab - 02 Nov 2006 16:21 GMT
> Hello,
>
> is there a way to parse a formatted date like '01Feb06' using
> SimpleDateFormat?
>
> "ddMMMyy" -> no

This seems to work.  Can you please post a SSCCE?

import java.io.PrintWriter;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {

    private static PrintWriter out = new PrintWriter(System.out, true);

    public static void main(String[] args) {
        String string = "01Feb06";
        SimpleDateFormat format = new SimpleDateFormat("ddMMMyy");
        ParsePosition index = new ParsePosition(0);
        Date date = format.parse(string, index);

        /* Prints "Wed Feb 01 00:00:00 EST 2006" */
        out.println(date);

    }

}
Kevin Mess - 02 Nov 2006 16:26 GMT
> Hello,
>
[quoted text clipped - 5 lines]
>
> TIA

Hi Denzel.  This is actually quite straightforward using
SimpleDateFormat.  You just need to make sure you use

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Test {

   public static void main(String[] args) throws ParseException {

       // set up a test string
       String testDate = "02Nov06";

       // create SimpleDateFormat object based on test string
       SimpleDateFormat format = new SimpleDateFormat("ddMMMyy");

       // display the parsed results.
       System.out.println("\"" + testDate + "\" parses to \"" +
format.parse(testDate) + "\"");
   }
}

OUTPUT:

"02Nov06" parses to "Thu Nov 02 00:00:00 PST 2006"

Process finished with exit code 0
Arne Vajhøj - 03 Nov 2006 03:09 GMT
> is there a way to parse a formatted date like '01Feb06' using
> SimpleDateFormat?
>
> "ddMMMyy" -> no

You have already seen a couple of answers saying that:

DateFormat format = new SimpleDateFormat("ddMMMyy");

work.

And it does - if the month abbreviations being parsed and
the locale settings on your PC match or if the month you are
testing on happens to be the same in the two locales.

DateFormat format = new SimpleDateFormat("ddMMMyy", Locale.ENGLISH);

then you will parse English month abbreviations independent
of your PC settings.

Arne
denzel - 03 Nov 2006 09:22 GMT
>> is there a way to parse a formatted date like '01Feb06' using
>> SimpleDateFormat?
[quoted text clipped - 15 lines]
> then you will parse English month abbreviations independent
> of your PC settings.

Thx to all of you. In fact it doesn't work, but it's a problem with the
French locale which does not seem to have the abreviated month (MMM). So
 i would have to replace the abreviated month name in complete month
name (Jan -> Janvier, etc.).

SimpleDateFormat("MMM").parse("Jan") doesn't work with French locale.

robert: using regular expression doesn't seem quite easy. I would a
format parser, which is what i don't want to develop (lazyness...). I
would prefer report a bug for the french locale.

Thanks for your help.
Arne Vajhøj - 04 Nov 2006 04:17 GMT
>> You have already seen a couple of answers saying that:
>>
[quoted text clipped - 17 lines]
>
> SimpleDateFormat("MMM").parse("Jan") doesn't work with French locale.

Try:

.parse("janv.")

Arne
denzel - 06 Nov 2006 17:34 GMT
> Try:
>
> ..parse("janv.")

It doesn't work. Only these works:

new java.text.SimpleDateFormat("MMM").parse("Janvier")
new java.text.SimpleDateFormat("MMMM").parse("Janvier")
Arne Vajhøj - 07 Nov 2006 02:18 GMT
>> Try:
>>
[quoted text clipped - 4 lines]
> new java.text.SimpleDateFormat("MMM").parse("Janvier")
> new java.text.SimpleDateFormat("MMMM").parse("Janvier")

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DF {
    public static void main(String[] args) throws Exception {
        String string = "01janv.06";
        SimpleDateFormat format = new SimpleDateFormat("ddMMMyy",
Locale.FRENCH);
        Date date = format.parse(string);
        System.out.println(date);
    }
}

works for me with SUN Java 1.5.0 !

Arne
denzel - 07 Nov 2006 12:52 GMT
> import java.text.SimpleDateFormat;
> import java.util.Date;
[quoted text clipped - 11 lines]
>
> works for me with SUN Java 1.5.0 !

sorry, i didn't see the dot.
Arne Vajhøj - 08 Nov 2006 00:41 GMT
>> import java.text.SimpleDateFormat;
>> import java.util.Date;
[quoted text clipped - 13 lines]
>
> sorry, i didn't see the dot.

Well - that dot is significant.

I don't understand the abbreviation, but then it is also
25 years since I last spoke french (hey - I was not even good at it).

Arne


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.