date format is not correct java

java date format
java date time format
default date format in java
string to date java
java date format mm/dd/yyyy
how to convert string to date in java in yyyy-mm-dd format
convert date format from yyyy-mm-dd to mm-dd/yyyy in java
how to convert string to a particular date format in java

I'm trying to read a date cell from excel file, i'm using DataFormatter but the problem is that the format that i get is not correct (day and month are inverted), example: in the excel file: 15/03/2018 After reading: 03/15/2018

I need to fix this or convert the cell to string format in order to compare it with another string.

thanks in advance.

This is how i read the cell:

DataFormatter formatter = new DataFormatter();
String current_date = formatter.formatCellValue(date_cell);

Try something like this:

String s = "03/15/2018";    
Date d = new Date(s);   
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String t = df.format(d);

(s corresponds to your current_date variable and follows directly after your other code)

NB: you have to use capital Ms because in date/time formats 'm' stand for minute (so my bad on previous code)

NB^2: the code "new Date(s)" is definitely deprecated and bad and wrong as per previous discussion. If it matters there's probably a more canonical answer involving essentially the same thing but with Calendars.

Change date format in a Java string, this way avoids confusion by placing the year first. Problem Description. How to check check whether date is in proper format or not? Solution. Following example demonstrates how to check whether the date is in a proper format or not using matches method of String class.

As seen in the javadocs at the top, Locale.US and Locale.UK can get mixed up.

After reading you get the US convention Month/Day/Year instead of the UK's one Day/Month/Year.

DataFormatter formatter = new DataFormatter(Locale.UK);

Correct Date Format | How To Write The Date In English, Java Date Validation Example: It checks if the entered date is valid or not. You can specify any date format and check the input dates against it. Now the code throws a java.time.format.DateTimeParseException: Text '2013-03-18 08:30' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {MonthOfYear=3, DayOfMonth=18, WeekBasedYear[WeekFields[SUNDAY,1]]=2013},ISO resolved to 08:30 of type java.time.format.Parsed. It’s long, I know.

You can build a specific date formatter, e.g.

SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");


So if it's reading the date correctly (by which I mean days are being read as days, and months are being read as months), then it's a simple matter of making a date formatter specific to the output you want.


SimpleDateFormat df = new SimpleDateFormat("dd/mm/yyyy");

and then something like ... uh ...

String current_date = df.format(date_cell);

or if you need the cell formatter to read the value for you it'll be a little bit more complex.

As an aside:

Note that since SimpleDateFormat is relatively simple and easy to use it is probably deprecated and regarded as bad, evil and wrong (tm).

(For the reason that once you start introducing things like time zones, localization and differences between calendar systems things become complex very quickly (because the real world is messy) and simple answers are only good for the quick and dirty 'local' fix)

How to check if Date is within a certain range in Java? –, Usually, SimpleDateFormat.parse() method is used for java date validation. Incorrect date validation example DateFormat.parse( Java Date Validation: Checks whether a Date is valid or not. In this example, we are checking whether a given date is valid or not. In the method validateJavaDate(String) we have specified the date format as “MM/dd/yyyy” that’s why only the date passed in this format is shown as valid in the output. You can specify any format of your choice and then check other formats against it.

Java SimpleDateFormat, In Java, you can use SimpleDateFormat class to check if a provided date is valid. setLenient(false); try { //if not valid, it will throw ParseException Date How to check date format only not date from user with the mm/dd/yyyy. Though using SimpleDateFormat is most easy way to format Date in Java but it also has its own set of problems. It's not thread-safe and should be used carefully. Avoid using DateFormat as static variable , don't share SimpleDateFormat between multiple threads, result is unpredictable if it's shared among multiple threads.

Java Date Validation Example, parse() method of SimpleDateFormat throws ParseException if String input is not a valid date or can not be parsed into mentioned format. How to format Date in  Formatting a Date Using a Custom Format: 6.12.28. Formatting date with full day and month name and show time up to milliseconds with AM/PM: 6.12.29. Format date in dd/mm/yyyy format: 6.12.30. Format date in mm-dd-yyyy hh:mm:ss format: 6.12.31. Formatting day of week using SimpleDateFormat: 6.12.32. Formatting day of week in EEEE format like

Java Strict Date Validation, The java.time APIs, available in the JDK 8 release, provides a comprehensive date and For a full description of the pattern's syntax, see the tables in Date Format That is, they will appear in the formatted text even if they are not enclosed  The answer is of course to create a SimpleDateFormat object and use it to parse Strings to Date and to format Dates to Strings. If you've tried SimpleDateFormat and it didn't work, then please show your code and any errors you may receive. Addendum: "mm" in the format String is not the same as "MM". Use MM for months and mm for minutes.

  • Are you using POI? If so, tag it apache-poi so we know
  • You're welcome :D The other commenters are probably right in that if you trawl through the POI/DataFormatter APIs enough you might find the thing to twiddle to make it come out just right, but sometimes it's easiest to just brute force the answer and move on with your life ;-) Also, I found a bug in our code while trying to help you: new SimpleDateFormat("yyyy-mm-dd"); Is always wrong (minutes/months confusion). Grrrr. Excuse me while I swat some bugs with extreme prejudice.
  • Thanks. i totaly agree with you, i was just going to surrender and use the split function to invert the day and the month haha, just to not stuck there ! you are a life saver (y)
  • FYI, the troublesome old date-time classes such as java.util.Date, java.util.Calendar, and java.text.SimpleDateFormat are now legacy, supplanted by the java.time classes built into Java 8 and later. See Tutorial by Oracle.
  • Yes Basil, that's what 'deprecated' means.
  • This is about Excel, not about Java date formatting.
  • It's tagged "java"
  • Look at the code. It's DataFormatter, not DateFormatter. It's tagged java because it's Java code, using the POI library to read Excel files.
  • You can take the value passed from DataFormatter and pump it through a DateFormatter, it's a useful technique in general.
  • Try taking your string from above, turning it into a Date (or DateTime (???)) object and then applying the formatting to that object.