How to convert a String to a Date using SimpleDateFormat?
330,888
Solution 1
Try this:
new SimpleDateFormat("MM/dd/yyyy")
-
MM
is "month" (notmm
) -
dd
is "day" (notDD
)
It's all in the javadoc for SimpleDateFormat
FYI, the reason your format is still a valid date format is that:
-
mm
is "minutes" -
DD
is "day in year"
Also, you don't need the cast to Date
... it already is a Date
(or it explodes):
public static void main(String[] args) throws ParseException {
System.out.println(new SimpleDateFormat("MM/dd/yyyy").parse("08/16/2011"));
}
Output:
Tue Aug 16 00:00:00 EST 2011
Voila!
Solution 2
m - min M - Months
Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day in week Text Tuesday; Tue
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
Solution 3
Use the below function
/**
* Format a time from a given format to given target format
*
* @param inputFormat
* @param inputTimeStamp
* @param outputFormat
* @return
* @throws ParseException
*/
private static String TimeStampConverter(final String inputFormat,
String inputTimeStamp, final String outputFormat)
throws ParseException {
return new SimpleDateFormat(outputFormat).format(new SimpleDateFormat(
inputFormat).parse(inputTimeStamp));
}
Sample Usage is as Following:
try {
String inputTimeStamp = "08/16/2011";
final String inputFormat = "MM/dd/yyyy";
final String outputFormat = "EEE MMM dd HH:mm:ss z yyyy";
System.out.println(TimeStampConverter(inputFormat, inputTimeStamp,
outputFormat));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Solution 4
String newstr = "08/16/2011";
SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat format = new SimpleDateFormat("EE MMM dd hh:mm:ss z yyyy");
Calendar c = Calendar.getInstance();
c.setTime(format1.parse(newstr));
System.out.println(format.format(c.getTime()));
Solution 5
Very Simple Example is.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = new Date();
Date date1 = new Date();
try {
System.out.println("Date1: "+date1);
System.out.println("date" + date);
date = simpleDateFormat.parse("01-01-2013");
date1 = simpleDateFormat.parse("06-15-2013");
System.out.println("Date1 is:"+date1);
System.out.println("date" + date);
} catch (Exception e) {
System.out.println(e.getMessage());
}
Author by
Shantanu Tomar
Updated on July 25, 2020Comments
-
Shantanu Tomar almost 4 years
I have this code snippet:
DateFormat formatter1; formatter1 = new SimpleDateFormat("mm/DD/yyyy"); System.out.println((Date)formatter1.parse("08/16/2011"));
When I run this, I get this as the output:
Sun Jan 16 00:10:00 IST 2011
I expected:
Tue Aug 16 "Whatever Time" IST 2011
I mean to say I am not getting the month as expected. What is the mistake?
-
Shantanu Tomar about 12 yearsNo luck.. :( Its still Jan.. No matter what value i give to MM it always displays me Jan. Date and year working fine..
-
Sefran2 about 11 years@Bohemian: If I use the same format (MM/dd/yyyy) with a wrong date (i.e. 02/40/2013), the parsing doesn't fail. I obtain 12 March 2013. Why? Is there a way to prevent this behaviour?
-
Bohemian about 11 years@Cricket the value is successfully parsed as the 40th day in February, simply rolling over to a day in March to accommodate the out of bounds value. To prevent this, call
setLenient(false);
on the format object before parsing (leniency is true by default). -
Fi Horan over 8 yearsThanks for the table, really helpful having it on this page
-
Ryan Augustine over 5 years"06-15-2013" should be replaced with "15-06-2013"