简体   繁体   中英

Java - SimpleDateFormat unexpected parsing behaviour

I'm reading information from a file (.csv) that will be inserted to a database after validation and approval from the end user. The text file is read, validated and its information loaded to a List containing forms which are used to check if the data already exist in database.

The problem arises when parsing the String to Date. The SimpleDateFormat.parse() method returns an unexpected date format even when the pattern for SimpleDateFormat is "yyyy-MM-dd".

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);

Parsing the loaded values to the travel object:

travel.setDate( dateFormat.parse( form.getTravelDate() ));

In debugger the form shows the date as:

"2012-12-12"

It is read as intended. However when parsed becomes:

Wed Dec 12 00:00:00 CST 2012

I've spent the whole day trying to solve this but I'm out of ideas at this point. Afaik the pattern is alright and I've tried adding a locale to no avail.

Edit: the problem is when I need to insert the values to the database using Hibernate. The not desired format also ends up showing in the Database.

The data is show in a .jsp page using

 HttpServletRequest("table",travelList);

The date format I don't need shows here, when in the past this issue never happened. At last the information is sent to the database where the problem persists.

No, it "becomes" a java.util.Date value. If you're then converting it back to a string by calling Date.toString() , you should consult the Date.toString() documentation for what to expect.

The value stored in the Date is just a point in time - the number of milliseconds since the Unix epoch. There's no format in there, no time zone etc. It also doesn't know that it was only a date value rather than a date and time (the naming of Date is one of the many unfortunate aspects of the API).

It's crucial that you mentally separate "the result of the parse operation" from "the string value that is used to represent that result if I call toString" ".

I'd also advise you to set the time zone on your SimpleDateFormat to UTC when parsing a date - that way you know that you can't possibly have any ambiguous or skipped times leading to hard-to-predict behaviour. (Of course, you then need to know that you'll have parsed the date to "the start of the UTC date" and handle it appropriately elsewhere.)

You need to use the formatter when printing the Date as well.

dateFormat.format( travel.getDate() );

When your parse the String using a DateFormat , you get a complete Date object with the time units missing in the string initialized to zero. In your example, that's hours, minutes and seconds.

By default, if you do not use a formatter, Date 's default string representation (provided by its toString() method) gets printed.

Parsing doesn't mean it format, it simply parse it as text to a java.util.Date object. See parse method in documentation .

You need to use the format method.

dateFormat.format(travel.getDate())

See documentation for more details.

if form.getTravelDate() is returning String then First Parse the Date from String

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
Date parsedDate=dateFormat.parse(form.getTravelDate()); 
// Here parsedDate is in form Wed Dec 12 00:00:00 CST 2012 

Now Format the Date using the Same SImpleDateFormat to get the desired output

System.out.println(dateFormat.format(parsedDate));

Your Desired Output

2012-12-12

Update

Assuming data Type of Columns in which we insert this data in Database is of Date Type not varchar then use the below statement

travel.setDate(dateFormat.format(parsedDate));

As I understand parse method returns Date . Below is the parse method syntax.

public Date parse(String source)
           throws ParseException

So, you need to parse the string date and store into a Date variable. Then format the Date variable using SimpleDateFormat .

//getTravelDate is "2012-12-12"

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
Date dtObj=new Date(); 
dtObj=dateFormat.parse(form.getTravelDate());   //Store in date variable    
System.out.println(dateFormat.format(dtObj)); // Now format the date object

The final output will be 2012-12-12 . Hope it will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM