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
?
如何将上面以YYYYMMDD格式表示的Integer转换为java.util.Date中的YYYY-MM-DD格式?
3 个解决方案
#1
13
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.
请记住,日期没有格式。它仅表示从1970-01-01开始的以毫秒为单位的特定实例。但是,如果要将该日期格式化为预期格式,则可以使用其他格式化程序。
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
现在你的formatedDate String应该包含以yyyy-MM-dd格式表示日期的字符串
#2
4
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();
#3
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
假设是1970年1月1日以来的时间
EDIT:-
编辑:-
If you want to convert from YYYYMMDD to YYYY-MM-DD format
如果要将YYYYMMDD转换为YYYY-MM-DD格式
Date dt = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH).parse(String.ValueOf(19000101));
#1
13
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.
请记住,日期没有格式。它仅表示从1970-01-01开始的以毫秒为单位的特定实例。但是,如果要将该日期格式化为预期格式,则可以使用其他格式化程序。
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
现在你的formatedDate String应该包含以yyyy-MM-dd格式表示日期的字符串
#2
4
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();
#3
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
假设是1970年1月1日以来的时间
EDIT:-
编辑:-
If you want to convert from YYYYMMDD to YYYY-MM-DD format
如果要将YYYYMMDD转换为YYYY-MM-DD格式
Date dt = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH).parse(String.ValueOf(19000101));