Date parsing without separator

D

denzel

Hello,

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

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

TIA
 
R

Robert Klemme

Hello,

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

"ddMMMyy" -> no
"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
 
J

Jeffrey Schwab

denzel said:
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);

}

}
 
K

Kevin Mess

Hello,

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

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

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
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

denzel said:
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
 
D

denzel

Arne said:
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.

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.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

denzel said:
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.

Try:

..parse("janv.")

Arne
 
D

denzel

Arne said:
Try:

..parse("janv.")

It doesn't work. Only these works:

new java.text.SimpleDateFormat("MMM").parse("Janvier")
new java.text.SimpleDateFormat("MMMM").parse("Janvier")
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

denzel said:
It doesn't work. Only these works:

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
 
D

denzel

Arne said:
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 !

sorry, i didn't see the dot.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

denzel said:
Arne said:
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 !

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,043
Members
48,769
Latest member
Clifft

Latest Threads

Top