Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3396916
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:27:56+00:00 2026-05-18T04:27:56+00:00

What is the best way to convert a String in the format ‘January 2,

  • 0

What is the best way to convert a String in the format ‘January 2, 2010’ to a Date in Java?

Ultimately, I want to break out the month, the day, and the year as integers so that I can use

Date date = new Date();
date.setMonth()..
date.setYear()..
date.setDay()..
date.setlong currentTime = date.getTime();

to convert the date into time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:27:56+00:00Added an answer on May 18, 2026 at 4:27 am

    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:

    1. "January" is the full text month, so use the MMMM pattern for it
    2. "2" is the short day-of-month, so use the d pattern for it.
    3. "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;

    Do note that it has several predefined formatters for the more popular patterns. So instead of e.g. DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);, you could use DateTimeFormatter.RFC_1123_DATE_TIME. This is possible because they are, on the contrary to SimpleDateFormat, thread safe. You could thus also define your own, if necessary.

    For a particular input string format, you don’t need to use an explicit DateTimeFormatter: a standard ISO 8601 date, like 2016-09-26T17:44:57Z, can be parsed directly with LocalDateTime#parse(text) as it already uses the ISO_LOCAL_DATE_TIME formatter. Similarly, LocalDate#parse(text) parses an ISO date without the time component (see ISO_LOCAL_DATE), and ZonedDateTime#parse(text) parses an ISO date with an offset and time zone added (see ISO_ZONED_DATE_TIME).


    Pre-Java 8

    In case you’re not on Java 8 yet, or are forced to use java.util.Date, then format the date using SimpleDateFormat using a format pattern matching the input string.

    String string = "January 2, 2010";
    DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
    Date date = format.parse(string);
    System.out.println(date); // Sat Jan 02 00:00:00 GMT 2010
    

    Note the importance of the explicit Locale argument. If you omit it, then it will use the default locale which is not necessarily English as used in the month name of the input string. If the locale doesn’t match with the input string, then you would confusingly get a java.text.ParseException even though when the format pattern seems valid.

    Here’s an extract of relevance from the javadoc, listing all available format patterns:

    Letter Date or Time Component Presentation Examples
    G Era designator Text AD
    y Year Year 1996; 96
    Y Week year Year 2009; 09
    M/L 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
    u Day number of week Number 1
    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
    X Time zone ISO 8601 time zone -08; -0800; -08:00

    Note that the patterns are case sensitive and that text based patterns of four characters or more represent the full form; otherwise a short or abbreviated form is used if available. So e.g. MMMMM or more is unnecessary.

    Here are some examples of valid SimpleDateFormat patterns to parse a given string to date:

    Input string Pattern
    2001.07.04 AD at 12:08:56 PDT yyyy.MM.dd G 'at' HH:mm:ss z
    Wed, Jul 4, ’01 EEE, MMM d, ''yy
    12:08 PM h:mm a
    12 o’clock PM, Pacific Daylight Time hh 'o''clock' a, zzzz
    0:08 PM, PDT K:mm a, z
    02001.July.04 AD 12:08 PM yyyyy.MMMM.dd GGG hh:mm aaa
    Wed, 4 Jul 2001 12:08:56 -0700 EEE, d MMM yyyy HH:mm:ss Z
    010704120856-0700 yyMMddHHmmssZ
    2001-07-04T12:08:56.235-0700 yyyy-MM-dd'T'HH:mm:ss.SSSZ
    2001-07-04T12:08:56.235-07:00 yyyy-MM-dd'T'HH:mm:ss.SSSXXX
    2001-W27-3 YYYY-'W'ww-u

    An important note is that SimpleDateFormat is not thread safe. In other words, you should never declare and assign it as a static or instance variable and then reuse it from different methods/threads. You should always create it brand new within the method local scope.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is best way to convert date from JavaScript string in format YYYYMMDD to
What is the best way to convert a string to DateTime? The format of
In Ruby, what's the best way to convert a string of the format: {
What is the best way to convert a string on the format 1-2-3-4 to
What is the best way to convert string to date in C# if my
Is this the best way to convert String hex to bytes? Or can you
What's the best way to convert a string to an enumeration value in C#?
What would be the best way to convert a 50-digit String to a BigInteger
How to specify the format string to convert the date alone from string. In
What is the best way to convert a (properly Json-formatted) String object into a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.