0

I have to following time-string: "23.02.2015 14:06:30 +01:00" which I want to print. The printed result should be: "23.02.2015 15:06:30". After some research I just found a solution which I dont think of to be a good idea. Perhabs anyone of you has some conclusions on how to do this right? The conversion should be done with joda-time. Thanks in advance.

** Update due to explaining difference for the mentioned dublicate: My first approach was just like mentioned in Java : how to add 10 mins in my Time to add or remove the hours manually. What I tried to underline is that I dont think of this as a good idea because this could be dangerous if the input string will change (much space for possible errors). So I was wondering if joda-time has some functionality that would make my life easier adn which I didnt notice so far.

Community
  • 1
  • 1
ccDict
  • 633
  • 2
  • 6
  • 17

2 Answers2

1

I also have noticed this strange situation when working and converting to Joda Time. What I did is just substract one hour from the Joda Time like this:

DateTime dt1 = new DateTime([YOUR_JAVA.UTIL.DATE]);

Date result = new Date(dt1).minus(Seconds.seconds(3600)).getSeconds() * 1000);
Moduo
  • 623
  • 6
  • 17
1

You can use DateTime class plusxx() methods;

public static void main(String[] args) {
        String dateTime = "23.02.2015 14:06:30 +01:00";
        DateTimeFormatter dtf = DateTimeFormat.forPattern("dd.MM.yyyy HH:mm:ss Z");

        DateTime jodatime = dtf.parseDateTime(dateTime);
        DateTimeFormatter dtfOut = DateTimeFormat.forPattern("dd.MM.yyyy HH:mm:ss");
        System.out.println(dtfOut.print(jodatime));


        DateTime resultDateTime = jodatime.plusHours(1);
        System.out.println(dtfOut.print(resultDateTime));
    }

And the output is;

23.02.2015 08:36:30
23.02.2015 09:36:30

Result date isn't wrong, for more information about this subject please look here.

For fixed dates, I added time zone with withZone( DateTimeZone.forID( "Europe/Prague" ) ;

public static void main(String[] args) {
        String dateTime = "23.02.2015 14:06:30 +01:00";
        DateTimeFormatter dtf = DateTimeFormat.forPattern("dd.MM.yyyy HH:mm:ss Z");

        DateTime jodatime = dtf.withZone( DateTimeZone.forID( "Europe/Prague" ) ).parseDateTime(dateTime);
        DateTimeFormatter dtfOut = DateTimeFormat.forPattern("dd.MM.yyyy HH:mm:ss");
        System.out.println(dtfOut.withZone( DateTimeZone.forID( "Europe/Prague" ) ).print(jodatime));


        DateTime resultDateTime = jodatime.plusHours(1);
        System.out.println(dtfOut.withZone( DateTimeZone.forID( "Europe/Prague" ) ).print(resultDateTime));
    }

And the output is ;

23.02.2015 14:06:30
23.02.2015 15:06:30
Community
  • 1
  • 1
Semih Eker
  • 2,389
  • 1
  • 20
  • 29
  • hi, thanks for yout post. so there is no other solution then adding / removing the hours manually? – ccDict Feb 24 '15 at 12:31
  • Common usage of this to use plusxxx methods, of course you can try some other ways but I think that is the easiest one. – Semih Eker Feb 24 '15 at 12:36