I have a requirement to convert String to Date (in dd-MM-yyyy format). But dateformat.parse gives the format with seconds. I need to convert the String date to Date in the same format as mentioned above.
-
3The Date class itself has no format. It is just a data type representing a specific date. If you want to output that date in a specific way you always have to convert it to String with a SimpleDateFormatter or something similar. – OH GOD SPIDERS Dec 04 '19 at 10:09
-
Can you please post your code? This sentence "But dateformat.parse gives the format with seconds." indicates you might have misunderstood what DateFormat is about. DateFormat.parse() gives you a Date, not a format. – Haris Osmanagić Dec 04 '19 at 10:09
-
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); String dateInString = "07-06-2013"; try { Date date = formatter.parse(dateInString); System.out.println(date); } catch (ParseException e) { e.printStackTrace(); } – galaxy0208 Dec 04 '19 at 10:09
-
I tried as above, but parsing gives in a format with seconds, I needed a date object which shows the format as 'dd-MM-yyyy' – galaxy0208 Dec 04 '19 at 10:12
-
1Possible duplicates of [Calendar date to yyyy-MM-dd format in java](https://stackoverflow.com/questions/12575990/calendar-date-to-yyyy-mm-dd-format-in-java/12576219#12576219) and [java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy](https://stackoverflow.com/questions/18480633/java-util-date-format-conversion-yyyy-mm-dd-to-mm-dd-yyyy/18480709#18480709) and probably a thousand others – MadProgrammer Dec 04 '19 at 10:14
-
2@ananya0208 You are still misunderstanding the whole thing. If you want to output a Date object in a specific way you need to format that date for example with the SimpleDateFormat.format method. What you are currenctly doing is just calling the toString method of the Date class , which will always output the date in a fixed way. – OH GOD SPIDERS Dec 04 '19 at 10:15
-
1*"I tried as above, but parsing gives in a format with seconds, I needed a date object which shows the format as 'dd-MM-yyyy'"* - `Date` is a container for the number of seconds since the Unix epoch, it DOES NOT have a concept of format. Instead you use a `DateTimeFormatter` to parse the value to a (in this case) `LocalDateTime` value and back again to a `String`, this is how the system works – MadProgrammer Dec 04 '19 at 10:16
-
I recommend you don’t use `DateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Dec 04 '19 at 15:08
3 Answers
If you do not need to use time of day or time zone, you can parse it by LocalDate
.
String str = "01-01-2000";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate date = LocalDate.parse(str, formatter);
Note that for day and time people most of the time would want a ZonedDateTime rather than a LocalDateTime. The name is counter-intuitive; the Local in both LocalDate
and LocalDateTime
means any locality in general rather than a specific time zone.
-
Please, for the love of every ones sanity, use the new `java.time` APIs instead – MadProgrammer Dec 04 '19 at 10:13
-
-
1I'd remove the reference to `SimpleDateFormat`, even if you're not using Java 8+, you can make use of the ThreeTen back port, which ports the `java.time` API for earlier versions of Java ... I'm been picky, but the community generally agrees, the old date/time APIs need to be buried ;) – MadProgrammer Dec 04 '19 at 10:32
-
I am correcting the code into using `LocalDate`. With `LocalDateTIme` I got `java.time.format.DateTimeParseException: Text '01-01-2000' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2000-01-01 of type java.time.format.Parsed`. – Ole V.V. Dec 07 '19 at 07:52
The class Date
will always contain both date a nd time information, since it represents an instant in time.
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ParsingDate {
public static void main(String[] args) {
DateFormat fmt = new SimpleDateFormat("dd-MM-yyyy");
Date d;
try {
d = fmt.parse("04-12-2019");
System.out.println(d); // Wed Dec 04 00:00:00 CET 2019
} catch (ParseException e) {
e.printStackTrace();
}
}
}
As you can see, hours, minutes, seconds and millis get all set to 0.
If you later want to output the date in string format, you need to use the DateFormat#format(Date)
method:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ParsingDate {
public static void main(String[] args) {
DateFormat fmt = new SimpleDateFormat("dd-MM-yyyy");
Date d = new Date();
System.out.println(d); // Wed Dec 04 11:24:35 CET 2019
System.out.println(fmt.format(d)); // 04-12-2019
}
}
If you'd rather store only date information, you could use the java.time
package and make use of LocalDate
.
LocalDate
stores only date information, since it does not represent an instant, rather a triple of year, month and date.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class ParsingLocalDate {
public static void main(String[] args) {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate d = LocalDate.parse("04-12-2019", fmt);
System.out.println(d); // 2019-12-04
}
}

- 1,222
- 2
- 15
- 31
-
2It should be noted that `java.util.Date` and `SimpleDateFormat` are considered effectively deprecated in favour of the `java.time` APIs (or the ThreeTen backport if you're still stuck on earlier versions of Java) – MadProgrammer Dec 04 '19 at 10:35
In most programming languages, date/time types are simply containers for the amount of time which has passed from a given point in time. They don’t have a format.
In the case of Java (AFAIR), time is measured in milliseconds since the Unix Epoch.
Since it's 2019, there is no excuse not to making use of the java.time
APIs (or the ThreeTen backport) and you should avoid using the, now effectively deprecated, older APIs
Parse String to LocalDate
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate localDate = LocalDate.parse("08-03-1972", inputFormatter);
Format LocalDate
to desired format
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd EEE MMM yyyy");
String value = localDate.format(outputFormatter);
System.out.println(value);
which outputs
08 Wed. Mar. 1972

- 81,772
- 15
- 137
- 161

- 343,457
- 22
- 230
- 366