如何在Java中将任何日期格式转换为所需的日期格式?

时间:2019-05-27 16:45:32

标签: java date date-formatting

用户可以在文本框中以任何格式提供日期(假设日期是本世纪)。用户提供的任何格式都可以转换为“ yyyyMMdd”格式。

我尝试使用以下自定义函数进行转换(但获取不同的日期以及超出限制的部分):

String description = "visit dates 1.04.2011, 1.1.2018, 01/02/03, 02-03-03, 03.04.03, 04/02/2004, 05-02-2004, 06.02.2004, 04/Feb/2005, 05-Feb-2005, 06.Feb.2005,  04th Feb, 2006, 05th Mar, 2006, 06th Apr, 2007,  February 1st, 2008, November 2nd, 2009, Dcember 5th 2010... ";

String months = "jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec";
String ddMMyyyy = "(\\d{1,2}[- /.]\\d{1,2}[- /.]\\d{4})";
String yyyyMMdd = "(\\d{4}[- /.]\\d{1,2}[- /.]\\d{1,2})";
String ddMMyy = "(\\d{1,2}[- /.]\\d{1,2}[- /.]\\d{2})";
String yyMMdd = "(\\d{2}[- /.]\\d{1,2}[- /.]\\d{1,2})";
String ddMMMyyyy = "(\\d{1,2}[- /.]["+months+"][- /.]\\d{4})";
String yyyyMMMdd = "(\\d{4}[- /.]["+months+"][- /.]\\d{1,2})";
...
Pattern ddMMyyyyPattern = Pattern.compile(ddMMyyyy);
Pattern yyyyMMddPattern = Pattern.compile(yyyyMMdd);
Pattern ddMMMyyyyPattern = Pattern.compile(ddMMMyyyy);
Pattern yyyyMMMddPattern = Pattern.compile(yyyyMMMdd);
Pattern ddMMyyPattern = Pattern.compile(ddMMyy);
Pattern yyMMddPattern = Pattern.compile(yyMMdd);
...
Matcher ddMMyyyyMatcher = ddMMyyyyPattern.match(description);
Matcher yyyyMMddMatcher = yyyyMMddPattern.match(description);
Matcher ddMMMyyyyMatcher = ddMMMyyyyPattern.match(description);
Matcher yyyyMMMddMatcher = yyyyMMMddPattern.match(description);
Matcher ddMMyyMatcher = ddMMyyPattern.match(description);
Matcher yyMMddMatcher = yyMMddPattern.match(description);
...
for(int g = 0; g < ddMMyyyyMatcher.groupCount(); g++){
while(ddMMyyyyMatcher.find()){
System.out.println(ddMMyyyyMatcher.group(g)+"\t"+ddmmyyyyToAppDate(ddMMyyyyMatcher.group(g)));
    }
}
...
public String getMonthNumber(String shortMonth){
    String month = "-1";
    try{
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat inputMonthFormat = new SimpleDateFormat("MMM");
        cal.setTime(inputMonthFormat.parse(shortMonth));
        SimpleDateFormat outputMonthFormat = new SimpleDateFormat("MM"); // 01-12
        month = outputMonthFormat.format(cal.getTime());
    }catch(Exception ex){
        System.out.println(ex);
    }
    return month;
}
...
public String ddmmyyyyToAppDate(String ddmmyyyyDate){
    char[] dateChars = ddmmyyyyDate.toCharArray();
    String appDate = "";
    for (int c = 6; c <= 9; c++){
    appDate +=Character.toString( dateChars[c]);
    }
    for (int c = 3; c <= 4; c++){
    appDate +=Character.toString( dateChars[c]);
    }
    for (int c = 0; c <= 1; c++){
    appDate +=Character.toString( dateChars[c]);
    }
    return appDate;
}

public String yyyymmddToAppDate(String yyyymmddDate){
    char[] dateChars = yyyymmddDate.toCharArray();
    String appDate = "";
    for (int c = 0; c <= 3; c++){
    appDate +=Character.toString( dateChars[c]);
    }
    for (int c = 5; c <= 6; c++){
    appDate +=Character.toString( dateChars[c]);
    }
    for (int c = 8; c <= 9; c++){
    appDate +=Character.toString( dateChars[c]);
    }
    return appDate;
}

public String ddMMMyyyyToAppDate(String ddMMMyyyyDate){
    char[] dateChars = ddMMMyyyyDate.toCharArray();
    String appDate = "";
    String month ="";
    for (int c = 7; c <= 10; c++){
        appDate += Character.toString( dateChars[c]);
    }
    for (int c = 3; c <= 5; c++){
        month +=Character.toString( dateChars[c]);
    }
    appDate += getMonthNumber(month);

    for (int c = 0; c <= 1; c++){
        appDate +=Character.toString( dateChars[c]);
    }
    return appDate;
}

public String yyyyMMMddToAppDate(String  yyyyMMMddDate){
    char[] dateChars = yyyyMMMddDate.toCharArray();
    String appDate = "";
    String month ="";
    for (int c = 0; c <= 3; c++){
        appDate += Character.toString( dateChars[c]);
    }
    for (int c = 4; c <= 6; c++){
        month +=Character.toString( dateChars[c]);
    }
    appDate += getMonthNumber(month);

    for (int c = 8; c <= 9; c++){
        appDate +=Character.toString( dateChars[c]);
    }
    return appDate;
}

如果可以在这方面使用任何内置功能,请指导我!

0 个答案:

没有答案