我有一个包含六个数字的字符串:650310
。它以 YYMMDD
格式表示 1965 March 10
。
是否有任何方法可以将这种格式识别为1965年3月10日
?
目前这是我的做法,效果不是很好。
public class Example {
public static void main(String args[]) {
//date in YYMMDD
//String x = "650310";
String x = "161020";
System.out.print(x.substring(4, 6)+" ");
if (Integer.parseInt(x.substring(2, 4)) == 10) {
System.out.print("October"+" ");
}
else if (Integer.parseInt(x.substring(2, 4)) == 03) {
System.out.print("March"+" ");
}
if (Integer.parseInt(x.substring(0, 2)) > 50) {
String yr = "19" + x.substring(0, 2);
System.out.println(yr);
} else if (Integer.parseInt(x.substring(0, 2)) < 50) {
String yr = "20" + x.substring(0, 2);
System.out.println(yr);
}
}
}
output : 20 October 2016
最佳答案
使用Java的SimpleDateFormat:
SimpleDateFormat inFormat = new SimpleDateFormat( "yyMMdd" );
Date theDate = format.parse( "650310" );
现在您有了一个 Date 对象,您可以使用它以其他格式显示日期:
SimpleDateFormat outFormat = new SimpleDateFormat( "dd MMMMM yyyy" );
StringBuffer output = outFormat.format( theDate );
使用output.toString()
显示新格式化的日期。祝你好运。
关于Java如何动态读取日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19784335/