我有一个有六个数字的字符串:650310。它以1965 march 10格式表示YYMMDD格式。
有什么方法可以将这种格式识别为10 march 1965吗?目前,这是我的方法,但不是很有效。
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发布于 2013-11-05 08:21:00
使用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()显示新格式化的日期。祝好运。
发布于 2013-11-05 08:26:55
试试这个例子
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Example {
public static void main(String args[]) throws ParseException {
SimpleDateFormat s = new SimpleDateFormat( "yyMMdd" );
Date theDate = s.parse( "650310" );
SimpleDateFormat p = new SimpleDateFormat( "dd MMMMM yyyy" );
System.out.println(p.format(theDate));
}
}输出1965年3月10日
发布于 2013-11-05 08:16:56
使用SimpleDateFormat进行日期解析。例如:
SimpleDateFormat format = new SimpleDateFormat("yyMMdd");
try {
System.out.println(format.parse("900310"));
} catch (ParseException e) {
e.printStackTrace();
}输出:星期六3月10日: 00:00:00
编辑:如果您想解析日期,尝试使用DateFormat获取Date!然后你可以用你自己的方式格式化它。我不同意你的反对意见。
SimpleDateFormat format = new SimpleDateFormat("yyMMdd");
try {
Date parse = format.parse("900310");
format.applyPattern("dd MMMM yyyy");
System.out.println(format.format(parse));
} catch (ParseException e) {
e.printStackTrace();
}产出10/Март/1990
https://stackoverflow.com/questions/19784335
复制相似问题