How to convert an integer (e.g. 19000101) to java.util.Date?

Here is my code:

Integer value = 19000101 ;         

How to convert the above integer represented in format YYYYMMDDto format YYYY-MM-DDin java.util.Date?

+4
source share
3 answers

First you must parse your format in the date object using the specified formatter

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

Remember that the date has no format. It simply represents a specific instance in time in milliseconds starting from 1970-01-01. But if you want to format this date in the expected format, you can use a different formatter.

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

formatedDate , yyyy-MM-dd

+13

, , , : , . .

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

Try the following:

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

Assuming this time is from 01/01/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));
+2
source

All Articles