use*_*944 72 java date date-format
我有一个java.util.Date格式yyyy-mm-dd.我想要它的格式mm-dd-yyyy
下面是我为此转换尝试的示例工具:
// Setting the pattern
SimpleDateFormat sm = new SimpleDateFormat("mm-dd-yyyy");
// myDate is the java.util.Date in yyyy-mm-dd format
// Converting it into String using formatter
String strDate = sm.format(myDate);
//Converting the String back to java.util.Date
Date dt = sm.parse(strDate);
Run Code Online (Sandbox Code Playgroud)
我得到的输出仍然不是格式mm-dd-yyyy.
请告诉我如何格式化java.util.Datefrom yyyy-mm-dd到mm-dd-yyyy
Mad*_*mer 140
Date 是自Unix时代(1970年1月1日00:00:00 UTC)以来的毫秒数的容器.
它没有格式的概念.
LocalDateTime ldt = LocalDateTime.now();
System.out.println(DateTimeFormatter.ofPattern("MM-dd-yyyy", Locale.ENGLISH).format(ldt));
System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ENGLISH).format(ldt));
System.out.println(ldt);
Run Code Online (Sandbox Code Playgroud)
输出...
05-11-2018
2018-05-11
2018-05-11T17:24:42.980
Run Code Online (Sandbox Code Playgroud)
你应该使用ThreeTen Backport
例如...
Date myDate = new Date();
System.out.println(myDate);
System.out.println(new SimpleDateFormat("MM-dd-yyyy").format(myDate));
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(myDate));
System.out.println(myDate);
Run Code Online (Sandbox Code Playgroud)
输出...
Wed Aug 28 16:20:39 EST 2013
08-28-2013
2013-08-28
Wed Aug 28 16:20:39 EST 2013
Run Code Online (Sandbox Code Playgroud)
没有格式化更改了基础Date值.这是DateFormatters 的目的
更新了其他示例
以防万一第一个例子没有意义......
此示例使用两个格式化程序来格式化相同的日期.然后我使用这些相同的格式化程序将String值解析回Dates.生成的解析不会改变Date报告其值的方式.
Date#toString只是它的内容的转储.您无法更改此设置,但您可以Date按照自己喜欢的方式格式化对象
try {
Date myDate = new Date();
System.out.println(myDate);
SimpleDateFormat mdyFormat = new SimpleDateFormat("MM-dd-yyyy");
SimpleDateFormat dmyFormat = new SimpleDateFormat("yyyy-MM-dd");
// Format the date to Strings
String mdy = mdyFormat.format(myDate);
String dmy = dmyFormat.format(myDate);
// Results...
System.out.println(mdy);
System.out.println(dmy);
// Parse the Strings back to dates
// Note, the formats don't "stick" with the Date value
System.out.println(mdyFormat.parse(mdy));
System.out.println(dmyFormat.parse(dmy));
} catch (ParseException exp) {
exp.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
哪个输出......
Wed Aug 28 16:24:54 EST 2013
08-28-2013
2013-08-28
Wed Aug 28 00:00:00 EST 2013
Wed Aug 28 00:00:00 EST 2013
Run Code Online (Sandbox Code Playgroud)
另外,请注意格式模式.仔细看看,SimpleDateFormat确保你没有使用错误的模式;)
new*_*ser 31
SimpleDateFormat("MM-dd-yyyy");
Run Code Online (Sandbox Code Playgroud)
代替
SimpleDateFormat("mm-dd-yyyy");
Run Code Online (Sandbox Code Playgroud)
因为MM points Month,mm points minutes
SimpleDateFormat sm = new SimpleDateFormat("MM-dd-yyyy");
String strDate = sm.format(myDate);
Run Code Online (Sandbox Code Playgroud)
dar*_*kat 14
'M'(资本)代表月和'm'(简单)代表分钟
几个月的一些例子
'M' -> 7 (without prefix 0 if it is single digit)
'M' -> 12
'MM' -> 07 (with prefix 0 if it is single digit)
'MM' -> 12
'MMM' -> Jul (display with 3 character)
'MMMM' -> December (display with full name)
Run Code Online (Sandbox Code Playgroud)
分钟的一些例子
'm' -> 3 (without prefix 0 if it is single digit)
'm' -> 19
'mm' -> 03 (with prefix 0 if it is single digit)
'mm' -> 19
Run Code Online (Sandbox Code Playgroud)
请将小写的“mm”月份改为大写的“MM”即可。下面是示例代码供参考。
Date myDate = new Date();
SimpleDateFormat sm = new SimpleDateFormat("MM-dd-yyyy");
String strDate = sm.format(myDate);
Date dt = sm.parse(strDate);
System.out.println(strDate);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
491231 次 |
| 最近记录: |