Methods that can be used to convert String to Date Type in Java programming language will be examined.
Method 1: Use DateTimeFormatter:
Works in java 8 and above versions.
Example 1:
Import DateTimeFormatter first.
import java.time.format.DateTimeFormatter;
Then enter the date format for the string you are going to translate. For this code to work, the format to be created with the given string value must be consistent.
String date = "12-10-2016"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d-M-yyyy"); System.out.println(formattedDate); System.out.println(formatter.format(formattedDate));
output:
2016-10-16 16-10-2016
Example 2:
String stringdate2 = "12-Aug-2016"; DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("d-MMM-yyyy", Locale.US); LocalDate formattedDate2 = LocalDate.parse(stringdate2, formatter2); System.out.println(formattedDate2); System.out.println(formatter.format(formattedDate2));
Example 3:
String stringdate3 = "Monday, Aug 12, 2019 13:25:31 PM"; DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("EEEE, MMM d, yyyy HH:mm:ss a", Locale.US); LocalDate formattedDate3 = LocalDate.parse(stringdate3, formatter3); System.out.println(formattedDate3); System.out.println(formatter.format(formattedDate3));
predefined formats
Formatter | Description | Example |
ofLocalizedDate(dateStyle) | Formatter with date style from the locale | '2011-12-03' |
ofLocalizedTime(timeStyle) | Formatter with time style from the locale | '10:15:30' |
ofLocalizedDateTime(dateTimeStyle) | Formatter with a style for date and time from the locale | '3 Jun 2008 11:05:30' |
ofLocalizedDateTime(dateStyle,timeStyle) | Formatter with date and time styles from the locale | '3 Jun 2008 11:05' |
BASIC_ISO_DATE | Basic ISO date | '20111203' |
ISO_LOCAL_DATE | ISO Local Date | '2011-12-03' |
ISO_OFFSET_DATE | ISO Date with offset | '2011-12-03+01:00' |
ISO_DATE | ISO Date with or without offset | '2011-12-03+01:00'; '2011-12-03' |
ISO_LOCAL_TIME | Time without offset | '10:15:30' |
ISO_OFFSET_TIME | Time with offset | '10:15:30+01:00' |
ISO_TIME | Time with or without offset | '10:15:30+01:00'; '10:15:30' |
ISO_LOCAL_DATE_TIME | ISO Local Date and Time | '2011-12-03T10:15:30' |
ISO_OFFSET_DATE_TIME | Date Time with Offset | 2011-12-03T10:15:30+01:00' |
ISO_ZONED_DATE_TIME | Zoned Date Time | '2011-12-03T10:15:30+01:00[Europe/Paris]' |
ISO_DATE_TIME | Date and time with ZoneId | '2011-12-03T10:15:30+01:00[Europe/Paris]' |
ISO_ORDINAL_DATE | Year and day of year | '2012-337' |
ISO_WEEK_DATE | Year and Week | 2012-W48-6' |
ISO_INSTANT | Date and Time of an Instant | '2011-12-03T10:15:30Z' |
RFC_1123_DATE_TIME | RFC 1123 / RFC 822 | 'Tue, 3 Jun 2008 11:05:30 GMT' |
Letter | The piece it represents | Example |
y | Year | 1996; 96 |
M | Month in year | July; Jul; 07 |
w | Week in year | 27 |
W | Week in the month | 2 |
D | Days in a year | 189 |
d | Day in the month | 10 |
F | Day of the week in the month | 2 |
E | Day of the week | Tuesday; Tue |
a | Morning / evening | PM |
H | Time in day (0-23) | 0 |
k | Hours during the day (1-24) | 24 |
K | Hour for morning / evening (0-11) | 0 |
h | Hour for morning / evening (1-12) | 12 |
m | Minute in hour | 30 |
s | Seconds in minutes | 55 |
S | Milliseconds | 978 |
Method 2: Using SimpleDateFormat
First, import transactions.
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date;
Then enter the date format for the string you are going to translate. For this code to work, the format to be created with the given string value must be consistent.
try { String stringdate4 = "2019/08/12"; SimpleDateFormat simpleformatter = new SimpleDateFormat("yyyy/MM/dd"); Date formattedDate4 = simpleformatter.parse(stringdate4); System.out.println(formattedDate4); System.out.println(simpleformatter.format(formattedDate4)); } catch (ParseException e) { e.printStackTrace(); }
Output:
Mon Aug 12 00:00:00 EEST 2019 2019/08/12
Tagged In:
Java