Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simpledateformat unparseable date

I have a String in a database (match.getDate) that has the following date format:

01/04/2018

This is the date I want to format, stored as day/month/year. I want to format this for my Android app.

I want to format the date into:

Sun 01 Apr 2018

My code below:

SimpleDateFormat fDate = new SimpleDateFormat("dd/MM/yyyy");
try {
    textViewDate.setText(fDate.parse(match.getDate()).toString());
} catch (ParseException ex) {
    System.out.println(ex.toString());
}

This outputs:

Sun Apr 08 00:00:00 GMT+00:00 2018.

I have also tried "EE, MM d, yyyy", but it gives me:

java.text.ParseException: Unparseable date: "01/04/2018"

like image 973
Carl Bruiners Avatar asked Mar 09 '18 11:03

Carl Bruiners


People also ask

Is SimpleDateFormat deprecated?

Class SimpleDateFormat. Deprecated. A class for parsing and formatting dates with a given pattern, compatible with the Java 6 API.

How do you handle Unparseable date exception in Java?

Basically, this exception occurs due to the input string is not correspond with the pattern. You can try the below format: SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy", Locale.

What can I use instead of SimpleDateFormat?

DateTimeFormatter is a replacement for the old SimpleDateFormat that is thread-safe and provides additional functionality.

How do I declare SimpleDateFormat?

Creating a SimpleDateFormat You create a SimpleDateFormat instance like this: String pattern = "yyyy-MM-dd"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); The pattern parameter passed to the SimpleDateFormat constructor is the pattern to use for parsing and formatting of dates.


2 Answers

The other answers solved your problem, but I think it's important to know some concepts and why your first attempt didn't work.

There's a difference between a date and a text that represents a date.

Example: today's date is March 9th 2018. That date is just a concept, an idea of "a specific point in our calendar system".

The same date, though, can be represented in many formats. It can be "graphical", in the form of a circle around a number in a piece of paper with lots of other numbers in some specific order, or it can be in plain text, such as:

  • 09/03/2018 (day/month/year)
  • 03/09/2018 (monty/day/year)
  • 2018-03-09 (ISO8601 format)
  • March, 9th 2018
  • 9 de março de 2018 (in Portuguese)
  • 2018年3月5日 (in Japanese)
  • and so on...

Note that the text representations are different, but all of them represent the same date (the same value).

With that in mind, let's see how Java works with these concepts.

  • a text is represented by a String. This class contains a sequence of characters, nothing more. These characters can represent anything; in this case, it's a date
  • a date was initially represented by java.util.Date, and then by java.util.Calendar, but those classes are full of problems and you should avoid them if possible. Today we have a better API for that.

In Android, you can use the java.time classes if available in the API level you're using, or the threeten backport for API levels lower than that (check here how to use it). You'll have easier and more reliable tools to deal with dates.

In your case, you have a String (a text representing a date) and you want to convert it to another format. You must do it in 2 steps:

  1. convert the String to some date-type (transform the text to numerical day/month/year values) - that's called parsing
  2. convert this date-type value to some format (transform the numerical values to text in a specific format) - that's called formatting

Why your attempts didn't work:

  • the first attempt gave you the wrong format because you called Date::toString() method, which produces an output (a text representation) in that format (Sun Apr 08 00:00:00 GMT+00:00 2018) - so the parsing was correct, but the formatting wasn't
  • in the second attempt, you used the output pattern (EE dd MMM yyyy, the one you should use for formatting) to parse the date (which caused the ParseException).

For step 1, you can use a LocalDate, a type that represents a date (day, month and year, without hours and without timezone), because that's what your input is:

String input = "01/04/2018";
DateTimeFormatter inputParser = DateTimeFormatter.ofPattern("dd/MM/yyyy");
// parse the input
LocalDate date = LocalDate.parse(input, inputParser);

That's more reliable than SimpleDateFormat because it solves lots of strange bugs and problems of the old API.

Now that we have our LocalDate object, we can do step 2:

// convert to another format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EE dd MMM yyyy", Locale.ENGLISH);
String output = date.format(formatter);

Note that I used a java.util.Locale. That's because the output you want has the day of week and month name in English, and if you don't specify a locale, it'll use the JVM's default (and who guarantees it'll always be English? it's better to tell the API which language you're using instead of relying on the default configs, because those can be changed anytime, even by other applications running in the same JVM).

And how do I know which letters must be used in DateTimeFormatter? Well, I've just read the javadoc.

like image 109
shind Avatar answered Oct 27 '22 01:10

shind


Use this date formatter method I have created

    public static String dateFormater(String dateFromJSON, String expectedFormat, String oldFormat) {
    SimpleDateFormat dateFormat = new SimpleDateFormat(oldFormat);
    Date date = null;
    String convertedDate = null;
    try {
        date = dateFormat.parse(dateFromJSON);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(expectedFormat);
        convertedDate = simpleDateFormat.format(date);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return convertedDate;
}

and call this method like

dateFormater(" 01/04/2018" , "EE dd MMM yyyy" , "dd/MM/yyyy") 

and you will get the desired output

like image 35
Harsh Patel Avatar answered Oct 27 '22 01:10

Harsh Patel