How can I convert an Integer (e.g 19000101 ) to java.util.Date?

83,150

Solution 1

First you have to parse your format into date object using formatter specified

Integer value = 19000101;
SimpleDateFormat originalFormat = new SimpleDateFormat("yyyyMMdd");
Date date = originalFormat.parse(value.toString());

Remember that Date has no format. It just represents specific instance in time in milliseconds starting from 1970-01-01. But if you want to format that date to your expected format, you can use another formatter.

SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd");
String formatedDate = newFormat.format(date);

Now your formatedDate String should contain string that represent date in format yyyy-MM-dd

Solution 2

It seems to me that you don't really have a number representing your date, you have a string of three numbers: year, month, and day. You can extract those values with some simple arithmetic.

Integer value = 19000101;
int year = value / 10000;
int month = (value % 10000) / 100;
int day = value % 100;
Date date = new GregorianCalendar(year, month, day).getTime();

Solution 3

Try this:

String myDate= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                          .format(new Date(19000101 * 1000L));

Assuming it is the time since 1/1/1970

EDIT:-

If you want to convert from YYYYMMDD to YYYY-MM-DD format

Date dt = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH).parse(String.ValueOf(19000101));

Solution 4

java.time

I recommend that you use java.time, the modern Java date and time API, for all of your date work.

I am assuming that you want January 1, 1900 from your integer value, 19000101. With java.time that’s a one-liner.

    Integer value = 19000101;
    LocalDate date = LocalDate.parse(
            value.toString(), DateTimeFormatter.BASIC_ISO_DATE);
    System.out.println(date);

Output:

1900-01-01

I am exploiting the fact that your format agrees with the basic ISO 8601 format for a date. There is a built-in formatter for this format. Which is very good news since writing our own format pattern strings is always error-prone.

I said java.util.Date

If you indispensably need an old-fashioned java.util.Date, typically for a legacy API not yet upgraded to java.time, the conversion is:

    Instant startOfDay = date.atStartOfDay(ZoneId.systemDefault()).toInstant();
    Date oldfashionedDate = Date.from(startOfDay);
    System.out.println(oldfashionedDate);

Example output:

Mon Jan 01 00:21:00 EET 1900

(The 21 minutes are due to a bug in the Date class. Such bugs exist for dates close to year 1900.)

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Java versions and on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Share:
83,150
sbanerjee
Author by

sbanerjee

Updated on July 10, 2022

Comments

  • sbanerjee
    sbanerjee almost 2 years

    Here's my code:

    Integer value = 19000101;         
    

    How can I convert the above Integer represented in YYYYMMDD format to YYYY-MM-DD format in java.util.Date?

  • Ray Toal
    Ray Toal over 9 years
    Not quite right, because parse takes a string. Otherwise, this answer has the right idea.
  • Rahul Tripathi
    Rahul Tripathi over 9 years
    @RayToal:- Thanks. Updated my answer!
  • Tariq Abbas
    Tariq Abbas over 3 years
    not working for me .my code is : DateTimeFormatter formatter=DateTimeFormatter.ofPattern("YYYYMMdd"); String date="20200913T12:00:00+0000".substring(0, 8).trim(); System.out.println("=======given date ====="+date);
  • Adi
    Adi about 3 years
    your format is not correct. Format is case sensitive. you need to use yyyyMMdd
  • Ole V.V.
    Ole V.V. over 2 years
    Thanks for wanting to contribute. Please don’t teach the young ones to use the long outdated and notoriously troublesome SimpleDateFormat class. At least not as the first option. And not without any reservation. We have so much better in java.time, the modern Java date and time API, and its DateTimeFormatter. Also I’m convinced that the OP expected 1 Jan 1900. From your code I got 1 Jan 1970, so 70 years off.
  • Ole V.V.
    Ole V.V. over 2 years
    @TariqAbbas For that string please see Java String to DateTime.
  • Ole V.V.
    Ole V.V. over 2 years
    I got Thu Feb 01 00:00:00 CET 1900. I don’t think February was intended. It’s how confusing the GregorianCalendar class is. I recommend we don’t use it.