How can I convert an Integer (e.g 19000101 ) to java.util.Date?
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
- Oracle tutorial: Date Time explaining how to use java.time.
- Wikipedia article: ISO 8601
-
Java Specification Request (JSR) 310, where
java.time
was first described. -
ThreeTen Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310). - Java 8+ APIs available through desugaring
- ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
sbanerjee
Updated on July 10, 2022Comments
-
sbanerjee almost 2 years
Here's my code:
Integer value = 19000101;
How can I convert the above
Integer
represented inYYYYMMDD
format toYYYY-MM-DD
format injava.util.Date
? -
Ray Toal over 9 yearsNot quite right, because
parse
takes a string. Otherwise, this answer has the right idea. -
Rahul Tripathi over 9 years@RayToal:- Thanks. Updated my answer!
-
Tariq Abbas over 3 yearsnot 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 about 3 yearsyour format is not correct. Format is case sensitive. you need to use
yyyyMMdd
-
Ole V.V. over 2 yearsThanks 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 injava.time
, the modern Java date and time API, and itsDateTimeFormatter
. 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. over 2 years@TariqAbbas For that string please see Java String to DateTime.
-
Ole V.V. over 2 yearsI got
Thu Feb 01 00:00:00 CET 1900
. I don’t think February was intended. It’s how confusing theGregorianCalendar
class is. I recommend we don’t use it.