9

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?

Lii
  • 11,553
  • 8
  • 64
  • 88
sbanerjee
  • 169
  • 1
  • 3
  • 8
  • what does that number represent? Is it time, or some format? – Adi Aug 23 '14 at 05:43
  • have you tried Date formatter? – Nazgul Aug 23 '14 at 05:48
  • this number is in YYYYMMDD format. I need YYYY-MM-DD format – sbanerjee Aug 23 '14 at 05:49
  • possible duplicate of [Parse string with integer value to date](http://stackoverflow.com/questions/14817898/parse-string-with-integer-value-to-date) – Basil Bourque Aug 23 '14 at 06:58
  • I recommend you don’t use `java.util.Date`. That class is poorly designed and long outdated. Instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jun 04 '20 at 17:40

5 Answers5

26

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

Adi
  • 2,364
  • 1
  • 17
  • 23
  • 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); – Tariq Abbas Sep 19 '20 at 18:19
  • 1
    your format is not correct. Format is case sensitive. you need to use `yyyyMMdd` – Adi Mar 11 '21 at 22:50
  • @TariqAbbas For that string please see [Java String to DateTime](https://stackoverflow.com/questions/18823627/java-string-to-datetime). – Ole V.V. Aug 09 '21 at 13:57
5

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();
Alexis King
  • 43,109
  • 15
  • 131
  • 205
  • 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. – Ole V.V. Aug 09 '21 at 15:19
2

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));
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

First, the value you provided should be long, then just do as follow:

Date date = new Date(19000101L);
SimpleDateFormat formatter = new SimpleDateFormat("d MMM yyyy", new Locale(<language>, <country>));

The default for Locale is English, so you can just ignore it.

Rida
  • 89
  • 1
  • 5
  • 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,](https://docs.oracle.com/javase/tutorial/datetime/) 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. Aug 09 '21 at 13:36