如何格式化从数据库中检索的日期
How to format the date retrieved from the database
我从数据库中检索了日期时间并将其保存到 ArrayList<String>
类型变量中。现在我想格式化这个 string.So 我需要先制作一个日期对象。但是当我制作日期对象时出现了一些问题。以下是我的代码。
Date date = new Date(bookingTime.get(i));
这里bookingTime
就是ArrayList
。谁能告诉我哪里出了问题..
使用 SimpleDateFormat class
更新
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
String myDate = "2015-12-09 15:36:00";
Date date = format.parse(myDate);
}
catch (ParseException e) {
e.printStackTrace();
}
这样就可以了。 :)
首先检查你从数据库中获取的日期,然后你可以根据你的数据库日期结构格式化日期。
Date date = new Date(bookingTime.get(i));
String formattedDate = new SimpleDateFormat("MM/dd/yyyy").format(date);
其他格式可用标准是,
"yyyy-MM-dd"
"yyyy-MM-dd HH:mm"
"yyyy-MM-dd HH:mmZ"
"yyyy-MM-dd HH:mm:ss.SSSZ"
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
使用以下代码行:-
String dtStart = "11/08/2013 08:48:10";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
try {
date = format.parse(dtStart); //// Here dtStart is yours bookingTime.get(i)
System.out.println("Date ->" + date);
} catch (Exception e) {
e.printStackTrace();
}
答案在这里...
String date = "2014-11-25 14:30";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:MM");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);
我从数据库中检索了日期时间并将其保存到 ArrayList<String>
类型变量中。现在我想格式化这个 string.So 我需要先制作一个日期对象。但是当我制作日期对象时出现了一些问题。以下是我的代码。
Date date = new Date(bookingTime.get(i));
这里bookingTime
就是ArrayList
。谁能告诉我哪里出了问题..
使用 SimpleDateFormat class
更新
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
String myDate = "2015-12-09 15:36:00";
Date date = format.parse(myDate);
}
catch (ParseException e) {
e.printStackTrace();
}
这样就可以了。 :)
首先检查你从数据库中获取的日期,然后你可以根据你的数据库日期结构格式化日期。
Date date = new Date(bookingTime.get(i));
String formattedDate = new SimpleDateFormat("MM/dd/yyyy").format(date);
其他格式可用标准是,
"yyyy-MM-dd"
"yyyy-MM-dd HH:mm"
"yyyy-MM-dd HH:mmZ"
"yyyy-MM-dd HH:mm:ss.SSSZ"
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
使用以下代码行:-
String dtStart = "11/08/2013 08:48:10";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
try {
date = format.parse(dtStart); //// Here dtStart is yours bookingTime.get(i)
System.out.println("Date ->" + date);
} catch (Exception e) {
e.printStackTrace();
}
答案在这里...
String date = "2014-11-25 14:30";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:MM");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);