Problem with parsing date strings

I'm trying to handle dates in different formats, but I'm not even able to get Java running the following example:

DateFormat df = DateFormat.getDateTimeInstance

(DateFormat.DEFAULT,

DateFormat.DEFAULT,

Locale.getDefault());

Date date = null;

try {

date = df.parse((new Date()).toString());

System.out.println (date.toString());

} catch (ParseException pe) {

System.out.println ("ERROR parsing : " +

pe.getMessage());

}

What is going on here? Can someone give my a hint of how to solve this date string parsing problem?

PS! I've tried with other DateFormat instances, but they all result in ParseException. Earlier versions of JDK had the possibility of doing something like:

Date date = new Date (oldStrDate);

How do I do this now?

[855 byte] By [nilspedersen] at [2007-9-26 3:38:16]
# 1
I think your life will be a lot easier if you use java.text.SimpleDateFormat directly.
neville_sequeira at 2007-6-29 12:11:43 > top of Java-index,Desktop,I18N...
# 2

import java.text.ParseException;

ParsePosition pos = new ParsePosition(0);

Date date = dfFrom.parse(dateString, pos);

where dateString is the date in String format...

if u want to convert Date object into String format...

String dateString = df.format(date);

where date is new Date()..

hope this helps

Zej

zej_2 at 2007-6-29 12:11:43 > top of Java-index,Desktop,I18N...
# 3

Hi,

Its easier for you to get the date and put into any format you want by going through two steps:

1. get the date as a double value hint useSystem.currentTimeMillis()

2. use the SimpleDateFormat to format the date, there is available examples on that on the following URL

http://java.sun.com/docs/books/tutorial/i18n/format/dateFormatSymbols.html

I hope that this is helpful

Fareed

fareedk2002 at 2007-6-29 12:11:43 > top of Java-index,Desktop,I18N...
# 4

Hi,

Its easier for you to get the date and put into any format you want by going through two steps:

1. get the date as a double value hint useSystem.currentTimeMillis()

2. use the SimpleDateFormat to format the date, there is available examples on that on the following URL

http://java.sun.com/docs/books/tutorial/i18n/format/dateFormatSymbols.html

I hope that this is helpful

Fareed

fareedk2002 at 2007-6-29 12:11:43 > top of Java-index,Desktop,I18N...
# 5

i'm having some problems with this, can anyone give any guidance ?

I have some code which uses SimpleDateFormat and ja_JP locale

and correctly formats the current date in String format. I can take this and parse it back into a date object using the same formatter. Same code works in Thai, Malay, German, both kinds of Chinese etc.

when I give another locale such as ru_RU, ar_JO it will correctly format a date for string output, but will not parse that String back in using the same dateformatter despite trying all available styles (LONG, FULL, SHORT) etc. It throws ParseExceptions(and I did also try setting Lenient).

I used the toPattern() method to verify that the patterns are what I think they are. I'm using the same code each time and checking the input and output in Java console so I know the characters arent being mangled.

I've had a good look round using Google and the various sun fora and databases and can't find any truly up-to-date about which exactly which locales are supported and how extensively, but I'd never have thought that Russian and Arabic would not be fully supported.

any thoughts ?

thanks

simexel at 2007-6-29 12:11:43 > top of Java-index,Desktop,I18N...
# 6

DateFormat supports ru_RU and ar_JO locales, I tried following code

in 1.4.2 and it does not give me exception, how does your code look like?

-x

import java.text.*;

import java.util.*;

public class fo {

public static void main(String[] argv) {

DateFormat df = DateFormat.getDateTimeInstance(

DateFormat.FULL,

DateFormat.FULL,

new Locale("ar","JO")); //new Locale("ru","RU"));

try {

Date date = new Date();

String dateS = df.format(date);

Date newDate = df.parse(dateS);

System.out.println("newDate=" + dateS);

} catch (Exception e) {

System.out.println("e=" + e);

}

}

}

xuemingshen at 2007-6-29 12:11:43 > top of Java-index,Desktop,I18N...