Reputation: 1712
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?
Upvotes: 71
Views: 354396
Reputation: 12757
You have used some type errors. If you want to set 08/16/2011 to following pattern. It is wrong because,
mm stands for minutes, use MM as it is for Months
DD is wrong, it should be dd which represents Days
Try this to achieve the output you want to get ( Tue Aug 16 "Whatever Time" IST 2011
),
String date = "08/16/2011"; //input date as String
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy"); // date pattern
Date myDate = simpleDateFormat.parse(date); // returns date object
System.out.println(myDate); //outputs: Tue Aug 16 00:00:00 IST 2011
Upvotes: 1
Reputation: 86148
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MM/dd/uuuu");
System.out.println(LocalDate.parse("08/16/2011", dateFormatter));
Output:
2011-08-16
I am contributing the modern answer. The answer by Bohemian is correct and was a good answer when it was written 6 years ago. Now the notoriously troublesome SimpleDateFormat
class is long outdated and we have so much better in java.time
, the modern Java date and time API. I warmly recommend you use this instead of the old date-time classes.
When I parse 08/16/2011
using your snippet, I get Sun Jan 16 00:08:00 CET 2011
. Since lowercase mm
is for minutes, I get 00:08:00
(8 minutes past midnight), and since uppercase DD
is for day of year, I get 16 January.
In java.time
too format pattern strings are case sensitive, and we needed to use uppercase MM
for month and lowercase dd for day of month.
Yes, java.time
works nicely on Java 6 and later and on both older and newer Android devices.
org.threeten.bp
with subpackages.java.time
.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).Upvotes: 2
Reputation: 10212
String localFormat = android.text.format.DateFormat.getBestDateTimePattern(Locale.getDefault(), "EEEE MMMM d");
return new SimpleDateFormat(localFormat, Locale.getDefault()).format(localMidnight);
will return a format based on device's language. Note that getBestDateTimePattern() returns "the best possible localized form of the given skeleton for the given locale"
Upvotes: 1
Reputation: 775
This piece of code helps to convert back and forth
System.out.println("Date: "+ String.valueOf(new Date()));
SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String stringdate = dt.format(new Date());
System.out.println("String.valueOf(date): "+stringdate);
try {
Date date = dt.parse(stringdate);
System.out.println("parse date: "+ String.valueOf(date));
} catch (ParseException e) {
e.printStackTrace();
}
Upvotes: 4
Reputation: 106
you can solve the problem much simple like First convert the the given string to the date object eg:
java.util.Date date1 = new Date("11/19/2015");
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd yyyy HH:mma");
String format = formatter.format(date);
System.out.println(format);
Upvotes: 3
Reputation: 489
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
Upvotes: 46
Reputation: 3439
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());
}
Upvotes: 4
Reputation: 6784
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();
}
Upvotes: 12
Reputation: 5047
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()));
Upvotes: 7
Reputation: 424973
Try this:
new SimpleDateFormat("MM/dd/yyyy")
MM
is "month" (not mm
)dd
is "day" (not DD
)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!
Upvotes: 99