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 7181537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:34:42+00:00 2026-05-28T17:34:42+00:00

The following Java code just parses a date (with time portion) 2009-01-28-09:11:12 using SimpleDateFormat

  • 0

The following Java code just parses a date (with time portion) 2009-01-28-09:11:12 using SimpleDateFormat. Let’s have look at it.

final public class Main
{
    public static void main(String[] args)
    {            
        try
        {
            DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            Date d = df.parse("2009-01-28-09:11:12");
            System.out.println(d);
        }
        catch (ParseException ex)
        {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

The date (with time) displayed (after parsing) by the above code is as follows,

Sun Nov 30 22:07:51 IST 2008

even though we are attempting to parse the date 2009-01-28-09:11:12. It looks somewhat wonky. Why does it parse so?

  • 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-28T17:34:42+00:00Added an answer on May 28, 2026 at 5:34 pm

    Shouldn’t your date format be something like this:

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
    

    to match this format:

    Date d = df.parse("2009-01-28-09:11:12");
    

    ?

    As for why, per this:

    • http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

    the parser actually looks at these as numbers, and the trick is that – is a part of a number, representing negative numbers. So if you do:

    df.parse("2009-01-02-00:00:00")
    

    it gives:

    Mon Dec 01 00:02:00 EST 2008
    

    That parses 2009 as yyyy, then -0 as MM (which is previous month as months start from 1), then 1 as dd, etc.

    As per parse in DateFormat:

    • http://docs.oracle.com/javase/6/docs/api/java/text/DateFormat.html#parse(java.lang.String)

    By default, parsing is lenient: If the input is not in the form used by this object’s format method but can still be parsed as a date, then the parse succeeds. Clients may insist on strict adherence to the format by calling setLenient(false).

    I guess, if you have an option, it would be better to use slashes instead of dashes, if you like formats like 2009/01/02 12:34:56. This:

    df.parse("2009/01/02-00:00:00")
    

    will throw an exception:

    ERROR java.text.ParseException:
    Unparseable date: "2009/01/02-00:00:00"
    

    I can only conclude it’s a very good thing that / is not considered a number division by DateFormat…

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

Sidebar

Related Questions

I have the following Java code: final Future future = exeService.submit( new Runnable() {
I have the following snippet of java code: final Class<?> junitCoreClass = AccessController.doPrivileged( new
If I have the following Java code: int[][] readAPuzzle() { Scanner input = new
Let's assume we've got the following Java code: public class Maintainer { private Map<Enum,
Say suppose I have the following Java code. public class Example { public static
Say I have the following piece of java code ArrayList<Double> myList = new Double[100];
I have the following Java code working with XPath to get data from XML
The following Java code is throwing a compiler error: if ( checkGameTitle(currGame) ) ArrayList<String>
Given the following Java code for generating a binary file: DataOutputStream out = new
When running the following Java code, I get very accurate and consistent results in

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.