将字符串日期转换为简单日期格式

时间:2013-11-08 17:12:04

标签: java android simpledateformat

我正在抓取一个包含此格式日期的网页:“2013年11月8日”。在我返回日期后,它们被组织成一个无序的字符串数组。我想要做的是以某种方式将这些字符串转换为简单的日期格式,如yyyy-MM-dd所以我可以订购它们并使用它们与日历进行交互?

4 个答案:

答案 0 :(得分:1)

这样的事情怎么样?

private String dateLongStringConvert(String dateLongString) {

    // split long date string into string array
    String[] dateArray = dateLongString.split(" ");

    // get day of month as an integer (strip out non numeric chars)
    int dayOfMonth = Integer.parseInt(dateArray[0].replaceAll("\\D+", ""));

    // Convert month string to number
    String month = "";
    switch (dateArray[1]) {
        case "January":
            month = "01";
        case "Feburary":
            month = "02";            
        case "March":
            month = "03";            
        case "April":
            month = "04";            
        case "May":
            month = "05";            
        case "June":
            month = "06";            
        case "July":
            month = "07";            
         case "August":
            month = "08";           
          case "September":
            month = "09";          
        case "October":
            month = "10";            
        case "Novemember":
            month = "11";
        case "December":
            month = "12";                
    }
    // return formated date string
    return dateArray[2] + "-" + month + "-" + String.format("%02d", dayOfMonth);
}

答案 1 :(得分:1)

String inputDate = "8th November 2013";
inputDate = inputDate.replaceAll("([0-9])st|nd|rd|th|\\.", "$1"); // get rid of the th.
Date date = new SimpleDateFormat("d MMM y", Locale.ENGLISH).parse(inputDate); // parse input date
String outputDate = new SimpleDateFormat("yyyy-MM-dd").format(date); // format to output date

答案 2 :(得分:0)

这样做的正确方法是使用像Stanford Temporal Tagger这样的解析器,并从文本中找出日期。团队提供了一个很好的GUI(http://nlp.stanford.edu:8080/sutime/process)来评估工具

答案 3 :(得分:0)

to_char(' YYYY / MM / DD HH24:MI:ss')