我希望从存储在字符串中的日期获得日名,日,月,年,如“Wed,27 feb 2013”

时间:2013-02-28 18:25:18

标签: java spring-mvc

“JAVA”我将日期存储在字符串中,例如“星期三,2013年12月27日”我想从字符串日期提取日期名称,日期,月份,年份我使用的是Spring MVC,如果有人知道解决方案,请指导我。或者如果有其他方式,那么请提前告诉我..谢谢。

4 个答案:

答案 0 :(得分:5)

这样做。没有JODA依赖:

DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy");
formatter.setLenient(false);
String dateStr = "Fri, 27 Dec 2013";
Date date = formatter.parse(dateStr);
Calendar calendar = Calendar.instance();
calendar.setTime(date);
// Get values from calendar.

答案 1 :(得分:2)

    DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy");
    String dateStr = "WED, 27 Dec 2013";
    Date date = formatter.parse(dateStr);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    // Get values from calendar.
    System.out.println(calendar.get(Calendar.DAY_OF_WEEK));//1 indexed starting from Sunday
    System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//0 indexed
    System.out.println(calendar.get(Calendar.MONTH));//0 indexed
    System.out.println(calendar.get(Calendar.YEAR));

答案 2 :(得分:0)

看看Joda时间。它提供了许多方法来解析日期,并在解析完日期后从中获取数据。

答案 3 :(得分:0)

由于星期几与所代表的日期不一致,我们会使用substring()剪切字符串并删除星期几。

public static void main(String argsp[]) {
    DateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
    formatter.setLenient(false);
    String dateStr = "Wed, 27 Dec 2013";
    String sub = dateStr.substring(4);
    try {
        Date date = formatter.parse(sub);
        System.out.println(date);
    } catch (Exception e) {
        e.printStackTrace();
    }
}