简体   繁体   中英

how to format a string value containing date to yyyy-mm-dd?

我有一个字符串变量,显示日期值为ddmmyyyy(04112013)。我想将此字符串值格式化为yyyy.mm.dd(2013.11.04)。这是在java中执行此操作的最佳方法吗?

You can use SimpleDateFormat#parse() to parse a String in a certain pattern into a Date.

String oldstring = "04112013";
Date date = new SimpleDateFormat("ddMMyyyy").parse(oldstring);

Use SimpleDateFormat#format() to format a Date into a String in a certain pattern.

String newstring = new SimpleDateFormat("yyyy.MM.dd").format(date);
System.out.println(newstring);

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM