How to parse month full form string using DateFormat in Java?
LocalDate from java.time
Use LocalDate
from java.time, the modern Java date and time API, for a date
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MMMM d, u", Locale.ENGLISH);
LocalDate date = LocalDate.parse("June 27, 2007", dateFormatter);
System.out.println(date);
Output:
2007-06-27
As others have said already, remember to specify an English-speaking locale when your string is in English. A LocalDate
is a date without time of day, so a lot better suitable for the date from your string than the old Date
class. Despite its name a Date
does not represent a date but a point in time that falls on at least two different dates in different time zones of the world.
Only if you need an old-fashioned Date
for an API that you cannot afford to upgrade to java.time just now, convert like this:
Instant startOfDay = date.atStartOfDay(ZoneId.systemDefault()).toInstant();
Date oldfashionedDate = Date.from(startOfDay);
System.out.println(oldfashionedDate);
Output in my time zone:
Wed Jun 27 00:00:00 CEST 2007
Link
Oracle tutorial: Date Time explaining how to use java.time.
Java: Convert String Date to Month Name Year (MMM yyyy)
Your format must match your input
for 2016-03-20
the format should be (just use a second SimpleDateFormat
object)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Full answer
SimpleDateFormat month_date = new SimpleDateFormat("MMM yyyy", Locale.ENGLISH);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String actualDate = "2016-03-20";
Date date = sdf.parse(actualDate);
String month_name = month_date.format(date);
System.out.println("Month :" + month_name); //Mar 2016
Using java.time
java-8
String actualDate = "2016-03-20";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ENGLISH);
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("MMM yyyy", Locale.ENGLISH);
DateTimeFormatter dtf3 = DateTimeFormatter.ofPattern("MMMM yyyy", Locale.ENGLISH);
LocalDate ld = LocalDate.parse(actualDate, dtf);
String month_name = dtf2.format(ld);
System.out.println(month_name); // Mar 2016
String fullMonthAndYear = dtf3.format(ld);
System.out.println(fullMonthAndYear); // March 2016
Java: How to convert string with month name to DateTime?
2018/Java 10
String value = "Sunday, September 29, 2013 7:59:58 AM PDT";
String format = "EEEE, MMMM dd, yyyy h:mm:ss a zzz";
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern(format)
.toFormatter(Locale.US);
ZonedDateTime zdt = ZonedDateTime.parse(value, formatter);
System.out.println(zdt);
System.out.println(zdt.format(formatter));
Which prints
2013-09-29T07:59:58-07:00[America/Los_Angeles]
Sunday, September 29, 2013 7:59:58 AM PDT
Original answer
You need to change the expected format to meet the requirements as specified by the SimpleDateFormat
String value = "Sunday, September 29, 2013 7:59:58 AM PDT";
String format = "EEEE, MMMM dd, yyyy h:mm:ss a zzz";
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date date = sdf.parse(value);
System.out.println(date);
System.out.println(sdf.format(date));
} catch (ParseException ex) {
ex.printStackTrace();
}
Which outputs...
Mon Sep 30 00:59:58 EST 2013
Sunday, September 29, 2013 7:59:58 AM PDT
How to parse date with only month and year with SimpleDateFormat
You parsed it fine, but it's printed in PDT, your local timezone.
Sat Oct 31 17:00:00 PDT 2020
Well, Date doesn't track timezones. The Calendar
class does, which is internal to the formatter. But still, default print behavior is current timezone.
If you logically convert this output back to UTC, and it will be November 1 since PDT is UTC-7.
Basically, use java.time
classes. See additional information here How can I get the current date and time in UTC or GMT in Java?
How can I convert a String date with full month names to a Date object in java?
String str = "6 December 2002";
DateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
Date date = (Date)formatter.parse(str);
- Here is working IDE One demo
Must See
- API Doc
Java string to date conversion
That's the hard way, and those java.util.Date
setter methods have been deprecated since Java 1.1 (1997). Moreover, the whole java.util.Date
class was de-facto deprecated (discommended) since introduction of java.time
API in Java 8 (2014).
Simply format the date using DateTimeFormatter
with a pattern matching the input string (the tutorial is available here).
In your specific case of "January 2, 2010" as the input string:
- "January" is the full text month, so use the
MMMM
pattern for it - "2" is the short day-of-month, so use the
d
pattern for it. - "2010" is the 4-digit year, so use the
yyyy
pattern for it.
String string = "January 2, 2010";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH);
LocalDate date = LocalDate.parse(string, formatter);
System.out.println(date); // 2010-01-02
Note: if your format pattern happens to contain the time part as well, then use LocalDateTime#parse(text, formatter)
instead of LocalDate#parse(text, formatter)
. And, if your format pattern happens to contain the time zone as well, then use ZonedDateTime#parse(text, formatter)
instead.
Here's an extract of relevance from the javadoc, listing all available format patterns:
Symbol | Meaning | Presentation | Examples |
---|---|---|---|
G | era | text | AD; Anno Domini; A |
u | year | year | 2004; 04 |
y | year-of-era | year | 2004; 04 |
D | day-of-year | number | 189 |
M /L | month-of-year | number/text | 7; 07; Jul; July; J |
d | day-of-month | number | 10 |
Q /q | quarter-of-year | number/text | 3; 03; Q3; 3rd quarter |
Y | week-based-year | year | 1996; 96 |
w | week-of-week-based-year | number | 27 |
W | week-of-month | number | 4 |
E | day-of-week | text | Tue; Tuesday; T |
e /c | localized day-of-week | number/text | 2; 02; Tue; Tuesday; T |
F | week-of-month | number | 3 |
a | am-pm-of-day | text | PM |
h | clock-hour-of-am-pm (1-12) | number | 12 |
K | hour-of-am-pm (0-11) | number | 0 |
k | clock-hour-of-am-pm (1-24) | number | 0 |
H | hour-of-day (0-23) | number | 0 |
m | minute-of-hour | number | 30 |
s | second-of-minute | number | 55 |
S | fraction-of-second | fraction | 978 |
A | milli-of-day | number | 1234 |
n | nano-of-second | number | 987654321 |
N | nano-of-day | number | 1234000000 |
V | time-zone ID | zone-id | America/Los_Angeles; Z; -08:30 |
z | time-zone name | zone-name | Pacific Standard Time; PST |
O | localized zone-offset | offset-O | GMT+8; GMT+08:00; UTC-08:00; |
X | zone-offset 'Z' for zero | offset-X | Z; -08; -0830; -08:30; -083015; -08:30:15; |
x | zone-offset | offset-x | +0000; -08; -0830; -08:30; -083015; -08:30:15; |
Z | zone-offset | offset-Z | +0000; -0800; -08:00; |
Parse date with month name in Joda-Time
Here a pure Joda answer:
DateTimeFormatter dtf =
DateTimeFormat.forPattern("MMMM dd yyyy").withLocale(Locale.ENGLISH);
LocalDate d1 = dtf.parseLocalDate("june 15 2015");
LocalDate d2 = dtf.parseLocalDate("september 15 2015");
System.out.println(d1.isBefore(d2)); // true
Related Topics
Why Is This Java Stream Operated Upon Twice
Getting an Attribute Value in Xml Element
Create a Jtds Connection String
Java, How to Implement a Shift Cipher (Caesar Cipher)
How to Convert a Byte Array to Its Numeric Value (Java)
Test for Floating Point Equality. (Fe_Floating_Point_Equality)
Arrayindexoutofboundsexception When Iterating Through All the Elements of an Array
Why Does 'Java -Version' Go to Stderr
How to Parse Case-Insensitive Strings with Jsr310 Datetimeformatter
Having a 3Rd Party Jar Included in Maven Shaded Jar Without Adding It to Local Repository
How to Avoid Type Safety Warnings with Hibernate Hql Results
In Which Case Do You Use the JPA @Jointable Annotation
Understanding Spring @Configuration Class
Handling Java Crypto Exceptions
How Often Should Connection, Statement and Resultset Be Closed in Jdbc
How to Tune Tomcat 5.5 Jvm Memory Settings Without Using the Configuration Program