简体   繁体   English

格式化日期从mm / dd / yyyy到MM DD,YYYY

[英]Formatting date from mm/dd/yyyy to MM DD, YYYY

For example it will format the date 2/15/2014 to February 15, 2014 例如,它将格式化日期为2014年2月15日至2014年2月15日

I have tried a switch to get the month and that works out, but I cant figure out how to get the rest after. 我已经尝试了一个转换来获得月份,但这是有效的,但我无法弄清楚如何完成剩下的工作。

This is what I have so far : 这是我到目前为止:

private void formatDate()
{
    String month = dateDeparture.substring(0, dateDeparture.indexOf('/'));
    switch(dateDeparture)
    {
    case 1:
        month = "January";
        break;
    case 2:
        month = "February";
        break;
    case 3:
        month = "March";
        break;
    case 4:
        month = "April";
        break;
    case 5:
        month = "May";
        break;
    case 6:
        month = "June";
        break;
    case 7:
        month = "July";
        break;
    case 8:
        month = "August";
        break;
    case 9:
        month = "September";
        break;
    case 10:
        month = "October";
        break;
    case 11:
        month = "November";
        break;
    default:
        month = "December";
        break;  
    }
    dateDeparture = month+" "+dateDeparture.substring(dateDeparture.indexOf('/'),  dateDeparture.lastIndexOf('/'))+ dateDeparture.substring(dateDeparture.lastIndexOf('/'));

}

Java has a class called SimpleDateFormat. Java有一个名为SimpleDateFormat的类。 Use that to achieve what you are doing. 用它来实现你正在做的事情。

Best would be to use a date formatting class like SimpleDateFormat: 最好的方法是使用像SimpleDateFormat这样的日期格式化类:

SimpleDateFormat numericDateFormatter = new SimpleDateFormat("MM/dd/yyyy");
Date date = numericDateFormatter.parse(dateDeparture);
SimpleDateFormat mixedDateFormatter = new SimpleDateFormat("MMMMMMMMM d, yyyy");
String dateNew = mixedDateFormatter.format(date);

So long as you are using Java 7, you can use String in switch statements... 只要您使用Java 7,就可以在switch语句中使用String ...

But, there are two problems with your current code... 但是,您当前的代码存在两个问题......

One, you are using dateDeparture and not month 一,你使用的是dateDeparture而不是month

switch(dateDeparture)

And your case statements are using int instead of String 而你的case语句使用的是int而不是String

case 1:

Instead, you need to use something more like... 相反,你需要使用更像......

switch (month) {
    case "1":
    case "01":
        month = "January";
        break;
}

Now, because you month "might" be zero indexed, you need to take into account both occurrences... 现在,因为你的月份“可能”被归零,你需要考虑两种情况......

If you're using Java 6 or eailer, you will need to convert the String value to an int ... 如果您使用的是Java 6或eailer,则需要将String值转换为int ...

int monthValue = Integer.parseInt(month);
switch (monthValue) {
    case 1:
        //...

A simpler approach would be to use the inbuilt APIs available to you... 一种更简单的方法是使用可用的内置API ...

Start by converting the String value to a Date value... 首先将String值转换为Date值...

SimpleDateFormat in = new SimpleDateFormat("MM/dd/yyyy");
Date date = in.parse(dateDeparture);

Then format the date... 然后格式化日期......

SimpleDateFormat out = new SimpleDateFormat("MMMM dd, yyyy");
String value = out.format(date);

Take a close look at SimpleDateFormat for more details... 仔细查看SimpleDateFormat以获取更多详细信息......

Do this... 做这个...

//splits string based on / delimiter 
String[] MDY = dateDeparture.split("/");
int month = Integer.parseInt(MDY[0]);
int day = Integer.parseInt(MDY[1]);
int year = Integer.parseInt(MDY[2]);

then use your switch statement on month and format day and year to your liking. 然后根据自己的喜好在月份上使用switch语句并格式化日期年份

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM