解析从文件读取的日期 [英] Parsing Date that is read from file
本文介绍了解析从文件读取的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在文件中记录为 17 Dec 2010 17:02:24 17 Dec 2010 18:02:24
。我正在从文件中读取这些文件....
我的解析器代码是:
I have record in the file as 17 Dec 2010 17:02:24 17 Dec 2010 18:02:24
. I am reading these from file....
my parser code is:
static SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy hh:mm:ss");
public static String DateFormat(String startdate) {
String date = null;
try {
java.util.Date tDate = df.parse(startdate);
df = new SimpleDateFormat("dd-MMM-yy hh:mm:ss a");
String formatteddate = df.format(tDate).toUpperCase();
return formatteddate;
} catch (ParseException e) {
System.out.println("Unable to Parse" + e);
}
return date;
}
但只有第一个日期格式被解析...然后错误将无法解析
but only first date format get parsed...then error will be unable to parse
推荐答案
您已经写过 df
再次使用 DateFormat(...)
方法中的不同格式(如下所示)。 df是一个静态变量,所以它将使用这种新的格式进行后续读取。 对于dd-MMM-yy hh:mm:ss a
you are over writing the df
value again with a different format (as shown below) in the DateFormat(...)
method. df is a static variable so it will use this new format for sub sequent reads. Use a new local variable for "dd-MMM-yy hh:mm:ss a"
df = new SimpleDateFormat("dd-MMM-yy hh:mm:ss a");
这篇关于解析从文件读取的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文