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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:15:16+00:00 2026-06-11T21:15:16+00:00

Following is a piece of code that I am running. @Test public void testMyMehotd()

  • 0

Following is a piece of code that I am running.

@Test
public void testMyMehotd() {
    String expected = "2012-09-12T20:13:47.796327Z";
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'");
    //df.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date d = null;
    try {
        d = df.parse(expected);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return;
    }

    String actual = df.format(d);

    System.out.println(expected);
    System.out.println(actual);

}

but the output is different than what I expect.

expected : 2012-09-12T20:13:47.796327Z
actual   : 2012-09-12T20:27:03.000327Z

Can someone tell me the reason for this and what is the solution.

Thanks in advance.

  • 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-06-11T21:15:17+00:00Added an answer on June 11, 2026 at 9:15 pm

    Whenever you exceed 999 milliseconds, DateFormat will try to add the remaining milliseconds to your date. Consider the following simpler example:

     String expected = "2012-09-12T20:13:47.1001Z";
     DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSS'Z'");
     Date d = df.parse(expected);
    

    The resulting date will be 2012-09-12T20:13:48.0001. That is, since you have 1001 milliseconds, you get 1 extra second (1000 milliseconds), and 1 millisecond (1001 % 1000). Thus instead of 47 seconds as in the original date, you get 48 seconds.


    This is also what happens if you try to parse a date with an invalid number of days in a month. For example, if you try to add an extra day to September, and parse 2012-09-31:

    String expected = "2012-09-31";
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    Date d = df.parse(expected);
    System.out.println(df.format(d));
    

    You’ll actually get 2012-10-01. Again, that’s because DateFormat will detect that the 31st day of September is not valid, and will try to use heuristics to transform the Date, thus adding one day, ending up with the first day of the next month.

    There’s an option to tell the parser not to use these heuristics, by setting lenient mode to false with:

    df.setLenient(false);
    

    However, using this mode, both above examples will throw a ParseException.

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

Sidebar

Related Questions

I'm running my unit tests on a piece of code that does the following
The following piece of code works as expected when running in a local install
When running the following piece of code, the execution of the Java String's native
I have the following piece of code that takes in some words, stores them
I have the following piece of code that generate a jquerymobile-style button <a href=#
I have the following piece of code that finds all elements in the document
I have the following piece of Java code that reads strings from CSV file.
I have the following piece of code from a function that takes the host
I have the following piece of code in my MainViewController. Is that the best
With the following piece of code, how do i know that anything was inserted

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.