如何将整数(例如19000101)转换为java.util.Date? [英] How can I convert an Integer (e.g 19000101 ) to java.util.Date?
问题描述
这是我的代码:
Integer value = 19000101 ;
如何转换上述以 YYYYMMDD
格式到 YYYY-MM-DD
格式在 java.util.Date
?
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());
请记住,Date没有格式。它仅代表从1970-01-01开始的以毫秒为单位的特定实例。但是,如果要将该日期格式化为您的预期格式,可以使用另一个格式化程序。
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);
现在您的 formatedDate
String应该包含字符串代表日期格式 yyyy-MM-dd
Now your formatedDate
String should contain string that represent date in format yyyy-MM-dd
这篇关于如何将整数(例如19000101)转换为java.util.Date?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!