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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:32:13+00:00 2026-05-17T19:32:13+00:00

I have the following problem using Joda-Time for parsing and producing date and time

  • 0

I have the following problem using Joda-Time for parsing and producing date and time around Daylight Saving Time (DST) hours. Here is an example (please, note that March 30th 2008 is Daylight Saving change in Italy):

DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime x = dtf.parseDateTime("30/03/2008 03:00:00");
int h = x.getHourOfDay();
System.out.println(h);
System.out.println(x.toString("dd/MM/yyyy HH:mm:ss"));
DateTime y = x.toDateMidnight().toDateTime().plusHours(h);
System.out.println(y.getHourOfDay());
System.out.println(y.toString("dd/MM/yyyy HH:mm:ss"));

I get the following output:

3
30/03/2008 03:00:00
4
30/03/2008 04:00:00

When i parse hour I get hour is 3. In my data structure I save the day storing midnight time, and then I have some value for each hour of the day (0-23). Then, when I write out the date, I re-compute the full date time making midnight plus hour. When I sum 3 hours to my midnight I get 04:00:00! And if I parse it again, I get hour 4!

Where is my mistake? Is there some way to get hour 2 when I parse or get hour three when I print out?

I have also tried to build output by hand:

String.format("%s %02d:00:00", date.toString("dd/MM/yyyy"), h);

but in this case for hour 2, I produce 30/03/2008 02:00:00 which is not a valid date (since hour 2 does not exist) and cannot be parsed any more.

Thank you in advance for your help.
Filippo

  • 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-17T19:32:13+00:00Added an answer on May 17, 2026 at 7:32 pm

    The data structure you are saving your data is not very optimal for the days with daylight saving time. Your day in this particular day should only have 23 hours.

    If you do:

        DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss").withLocale(Locale.US);
        DateTime x = dtf.parseDateTime("30/03/2008 00:00:00");
    
        DateTimeFormatter parser = DateTimeFormat.fullDateTime();
        System.out.println("Start:"+parser.print(x));
    
        DateTime y = x.plusHours(4);
    
        System.out.println("After add of 4:"+parser.print(y));
    

    You get the expected result, that the time is 05:00.

    I recommend that you change the way you store your day and use a date. If not, you must handle daylight saving time when storing the hour of day.

    You might do something like this:
    In the case where we move the time forward one hour, as this case, you must store 4 and not 5 as the time for 5. And when you calculate the time, you should use the plusHours() method to get the actual time. I think you might get away with something like:

    public class DateTest {
        private static final int HOUR_TO_TEST = 2;
    
      public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
        DateTime startOfDay = dtf.parseDateTime("30/03/2008 00:00:00");
    
        /* Obtained from new DateTime() in code in practice */
        DateTime actualTimeWhenStoring = startOfDay.plusHours(HOUR_TO_TEST);
    
        int hourOfDay = actualTimeWhenStoring.getHourOfDay();
        int hourOffset = startOfDay.plusHours(hourOfDay).getHourOfDay();
    
        System.out.println("Hour of day:" + hourOfDay);
        System.out.println("Offset hour:" + hourOffset);
    
        int timeToSave = hourOfDay;
        if (hourOffset != hourOfDay) {
            timeToSave = (hourOfDay + (hourOfDay - hourOffset));
        }
        System.out.println("Time to save:" + timeToSave);
    
        /* When obtaining from db: */
        DateTime recalculatedTime = startOfDay.plusHours(timeToSave);
    
        System.out.println("Hour of time 'read' from db:" + recalculatedTime.getHourOfDay());
      }
    }
    

    …or basicly something like that. I’d write a test for it if you choose for going down this route. You can change the HOUR_TO_TEST to see that it moves passed the daylight saving time.

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

Sidebar

Related Questions

I have the following problem using subversion: I'm currently working on the trunk of
I have the following problem using template instantiation [*]. file foo.h class Foo {
I have the following problem in my Data Structures and Problem Solving using Java
I have a problem with the following Linq query using Entity Framework: from o
I have the following problem: I'm using the wsimport ant task to create a
I have the following problem: Using asp.net C# and calling several places in code
I have problem using Fluent Nhibernate, I have following model. When I try to
ich have the following problem: I'm using a the construct of the default Navigation
I have the following problem: A program displays a picture using a PictureBox .
I have the following problem .. I am using StackLayoutPanel and when I try

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.