1

I'm trying to convert data from string to Data class so I can later compare it to another data.

My data format: dd-MM-yyyy (ex. 31-07-2019).

The problem is that after format.parse("string date") operation it shows me the wrong data format:

Wed Jul 31 00:00:00 UTC 2019

Here is my code:

import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.*;

public class Program {

    public static void main(String[] args) {
        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");

        try {
            String dateString = format.format(new Date());
            String dateStr = "31-07-2019";
            Date date = format.parse(dateStr);

            System.out.println(dateString);
            System.out.println(date);
        } catch (ParseException e) {
            System.out.println("ParseError " + e.getMessage());
        }       
    }
}

dateString (which is the current date) parses successfully.

deHaar
  • 17,687
  • 10
  • 38
  • 51
Karen
  • 1,249
  • 4
  • 23
  • 46
  • 4
    Basically when you are calling `System.out.println(date);`, you are calling `System.out.println(date.toString());` - If you look at `Date`'s `toString` method you will that it is of course not using `SimpleDateFormat` – Scary Wombat Aug 02 '19 at 08:08
  • how I can manage that? – Karen Aug 02 '19 at 08:10
  • 1
    If you're just worried about how the date is printed, that's not a parsing problem at all. It's a formatting problem. – khelwood Aug 02 '19 at 08:15
  • `SimpleDateFormat` is notoriously troublesome, and `Date` is poorly designed too. Both are long outdated. I recommend that instead you use the modern `LocalDate` and `DateTimeFormatter`. No matter if we're talking `Date` or `LocalDate`, they haven't got, as in cannot have a format. Since you wanted them to be able to compare them, this fact shouldn't be of any concern to you. – Ole V.V. Aug 02 '19 at 11:03

3 Answers3

5

Use java.time instead of java.util for dates and times along with formatting and parsing.

public static void main(String args[]) throws Exception {
    // create a custom formatter for your pattern       
    DateTimeFormatter euroDtf = DateTimeFormatter.ofPattern("dd-MM-yyyy");
    // receive today's date
    LocalDate today = LocalDate.now();
    // parse a date that has the form of your pattern using your custom formatter
    LocalDate parsedDate = LocalDate.parse("31-07-2019", euroDtf);

    System.out.println("Today is " + today.format(euroDtf));
    System.out.println("Parsed date is " + parsedDate.format(euroDtf));
}
deHaar
  • 17,687
  • 10
  • 38
  • 51
3

I would say SimpleDateFormat is legacy, use the jdk-8 LocalDate

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate date1 = LocalDate.parse("31-07-2019",formatter);
LocalDate date2 = LocalDate.now();

And also you can use isBefore, isAfter for comparing

date1.isAfter(date2);
date2.isBefore(date2);

By default LocalDate returns the date in ISO-8601 format

A date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.

After comparison you can format the LocalDate into string by using same formatter

String date2 = LocalDate.now().format(formatter);

SimpleDateFormat.parse returns java.util.Date object

public Date parse(String source) throws ParseException

And Date.toString() represents the string of pattern of

public String toString()

 Converts this Date object to a String of the form:

     dow mon dd hh:mm:ss zzz yyyy
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
1

That is the expected behaviour

The class Date represents a specific instant in time, with millisecond precision.

format() will generate a String representation of date in the "format". parse() will return a Date Object which will always be in "Fri Aug 02 16:14:21 SGT 2019" format.

It’s important to note here that the pattern supplied in the constructor should be in the same format as the date parsed using the parse method.

Neha Jirafe
  • 741
  • 5
  • 14