我应该使用并行数组来自动简化格式化日期吗?

时间:2013-02-03 00:49:55

标签: java date-formatting

我是Java编程的新手。

我希望用户输入system.out.println个日期,并让它知道它是'st','th','rd'还是'nd'。因此,如果我输入'13'作为我出生的日期,它会将'th'添加到它以使其成为'第13'。

如何使用所有数字'1'到'31'自动执行此操作?

我应该使用并行数组并将一部分设置为'1'到'31'[0] - [30]而部分设置为[0] - [3]用于st,nd,rd,th?并让它们相应匹配?

如果是,或者如果不是,我该怎么声明呢?

抱歉这个可怕的问题。我很难理解我的想法。

6 个答案:

答案 0 :(得分:1)

我会使用switch语句

 int day;
    // no read in or assign day: 1 - 31

String ext;
// day % 10 (modulo 10) reduces to 0-9
switch (day % 10) {
case 1: ext = "st"; 
  break;
case 2: ext = "nd"; 
  break;
case 3: ext = "rd"; 
  break;
default: ext = "th";
  break;
}
if (day >= 11 && day <==13) ext == "th";
String dayText = "day: " + day + ext;

答案 1 :(得分:1)

public static void main(String[] args) {
        String[] dates = {"","1st","2nd","3rd","4th",...,"31st"};
        int input = 24;
        System.out.println(dates[input]);
}

答案 2 :(得分:1)

我会这样做:

String getExtension(int day) {

  switch(day) {

    case 1:
    case 21:
    case 31:
    return "st";

    case 2:
    case 22:
    return "nd";

    case 3:
    case 23:
    return "rd";

    default:
    return "th";
  }

}


String formatDay(int day) {
   return day + getExtension(day);
}

答案 3 :(得分:0)

我建议使用switch语句,但请务必将其命令为包含11,12和13。

switch (day) {
case 2:
case 22:
    suffix = "nd";
    break;
case 3:
case 23:
    suffix = "rd";
    break;
default:
    //Accounts for all other numbers including 1, 11, 12, 13, 21, and 31
    suffix = "st";
    break; //Because I'm a conformist.
}

答案 4 :(得分:0)

public class SourceCodeProgram {

    public static void main(String[] args) {
        System.out.println(OrdinalNumber.format(1));
        System.out.println(OrdinalNumber.format(2));
        System.out.println(OrdinalNumber.format(3));
        System.out.println(OrdinalNumber.format(4));
        System.out.println(OrdinalNumber.format(5));
        System.out.println(OrdinalNumber.format(6));
        System.out.println(OrdinalNumber.format(12));
        System.out.println(OrdinalNumber.format(23));
        System.out.println(OrdinalNumber.format(101));
    }
}

class OrdinalNumber {

    public static String format(int value) {
        if (value > 3 && value < 21) {
                        //exception
            return value + "th";
        }
        switch (value % 10) {
        case 1:
            return value + "st";
        case 2:
            return value + "nd";
        case 3:
            return value + "rd";
        default:
            return value + "th";
        }
    }
}

此外,请参阅:

答案 5 :(得分:0)

我认为你不需要这样做。关于这种日期的附属物有明确的规则。

  • 如果日期以1结尾且小于10或大于20,则以'st'结尾。
  • 如果日期以2结尾且小于10或大于20,则以'nd'结尾。
  • 如果日期以3结尾且小于10或大于20,则以'rd'结尾。
  • 所有其他日期以'th'结尾。

这是我过度设计的解决方案。从中获取你想要的东西。

public String appendDateEnding(String theDate) {
    if(null != theDate) {
        try {
            return appendDateEnding(Integer.parseInt(theDate));
        } catch (NumberFormatException e) {
            // we'll return null since we don't have a valid date.
        }
    }
    return null;
}

private String appendDateEnding(int theDate) {
    StringBuffer result = new StringBuffer();
    if(32 <= theDate || 0 >= theDate) {
        throw new IllegalArgumentException("Invalid date.");
    } else {
        result.append(theDate);
        int end = theDate % 10;
        if(1 == end && isValidForSuffix(theDate)) {
            result.append("st");
        } else if(2 == end && isValidForSuffix(theDate)) {
            result.append("nd");
        } else if(3 == end && isValidForSuffix(theDate)) {
            result.append("rd");
        } else {
            result.append("th");
        }
    }
    return result.toString();
}

private boolean isValidForSuffix(int theDate) {
    return (10 > theDate || 20 < theDate);
}

如果给出1到31之间的天数,它打印出来的内容:

1st
2nd
3rd
4th
5th
6th
7th
8th
9th
10th
11th
12th
13th
14th
15th
16th
17th
18th
19th
20th
21st
22nd
23rd
24th
25th
26th
27th
28th
29th
30th
31st