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
?
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
?
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
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();
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));
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.
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.)
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.
org.threeten.bp
with subpackages.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).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.