0

I'm trying to initialise a Joda-Time DateTime object with the hour of 12:00 here is how I do this:

public static final long MINUTE         = 60 * 1000;
public static final long HOUR           = 60 * MINUTE; 

DateTime defaultDate = new DateTime(HOUR * 12);
System.out.print("the hour is: " + defaultDate.getHourOfDay()) // getting 14

Why I am getting 14 and not 12? Maybe Mama didn't teach me how to read clock right?!

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
vlio20
  • 8,955
  • 18
  • 95
  • 180
  • Your question is unclear. You should explain what you were trying to do, what you expected, and what is wrong. Were you trying to create a date-time of noon on the first day of 1970 in your local time zone? That's what your code seems to suggest. I suggest you read the [Joda-Time](http://www.joda.org/joda-time/) documentation and search StackOverflow for "joda" to find many examples. – Basil Bourque Feb 04 '14 at 08:34

2 Answers2

6

You're specifying a number of milliseconds since the Unix epoch, which was midnight UTC.

However, you're implicitly using the system default time zone in your DateTime, and I suspect that at the Unix epoch, your system time zone was UTC+2.

If you want to use a specific time zone, you can pass that in the constructor:

DateTime defaultDate = new DateTime(HOUR * 12, DateTimeZone.UTC);

Also, rather than using your own constants, you could either use DateTimeConstants.MILLIS_PER_HOUR or use java.util.concurrent.TimeUnit for conversions.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • So is there any way to do this without the UTC? – vlio20 Feb 03 '14 at 10:20
  • 4
    @VladIoffe: Do *what* exactly? I suspect you haven't thought about precisely what type of object you're creating. If you want 12am of January 1st 1970 in your local time zone, I suggest you create a `LocalDateTime` first. – Jon Skeet Feb 03 '14 at 10:21
1

java.time

Quoted below is a notice from the home page of Joda-Time:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Solution using java.time, the modern Date-Time API:

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        ZonedDateTime zdt = ZonedDateTime.of(LocalDateTime.of(LocalDate.EPOCH, LocalTime.of(12, 0)), ZoneOffset.UTC);
        System.out.println(zdt);
    }
}

Output:

1970-01-01T12:00Z

ONLINE DEMO

A couple of important notes:

  1. ZonedDateTime#toString removes seconds and fraction-of-second if they are zero. If you want to display them, you can use DateTimeFormatter e.g.
String formatted = zdt.format(DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH));
  1. The Z in the output is the timezone designator for zero-timezone offset. It stands for Zulu and specifies the Etc/UTC timezone (which has the timezone offset of +00:00 hours).

Learn more about the modern Date-Time API from Trail: Date Time.

What went wrong with your code?

Quoted below the is description of DateTime(long) with my emphasis:

Constructs an instance set to the milliseconds from 1970-01-01T00:00:00Z using ISOChronology in the default time zone.

Your place, Israel was at an offset of +02:00 hours in 1970 and therefore the DateTime instance was instantiated with an offset of +02:00 hours.

Demo:

import java.util.concurrent.TimeUnit;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

public class Main {
    public static void main(String[] args) {
        DateTime defaultDate = new DateTime(TimeUnit.HOURS.toMillis(12), DateTimeZone.UTC);
        System.out.println(defaultDate);
    }
}

Output:

1970-01-01T12:00:00.000Z

Another thing, which you might have already noticed from the code, is that DO NOT perform calculations yourself if there is already a standard API (e.g. TimeUnit#toMillis) available for the same.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110