java 我想从存储在字符串中的日期获取日名称、日、月、年,例如“2013 年 2 月 27 日星期三”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15142860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:38:09  来源:igfitidea点击:

i want to get day name , day , month ,year from the date stored in String like "Wed, 27 feb 2013"

javaspring-mvc

提问by user2120794

"JAVA" i stores Date in String like "wed, 27 Dec 2013" i want to extract day name , day , month, year from String date i am using spring MVC ,please guide me if anyone know solution . or if there are other way then also please suggest me ..thanks in advance.

“JAVA”我将日期存储在字符串中,例如“2013 年 12 月 27 日星期三”我想从字符串日期中提取日期名称、日期、月份、年份我正在使用 spring MVC,如果有人知道解决方案,请指导我。或者如果有其他方式,也请建议我..提前致谢。

回答by duffymo

This will do it. No JODA dependency:

这将做到。没有 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.

回答by Deb

    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));

回答by user000001

Since the day of the week is inconsistent with the date represented, we will cut the string, and remove the day of the week, using substring().

由于星期几与表示的日期不一致,我们将剪切字符串,并使用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();
    }
}

回答by jgm

Take a look at Joda time. It provides lots of methods for parsing dates, and obtaining data from them once they have been parsed.

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