【问题标题】:Incrementing month递增月份
【发布时间】:2020-01-20 05:23:44
【问题描述】:

我有一个包含一个月的字符串。

String mon = "January";

现在,另一个字符串 nextMon 应该会在下个月保留。也就是说,如果mon"January",则nextMon 应该是"February"

我从函数中得到mon 字符串,所以它必须自动发生。

请告诉我最简单的方法。

【问题讨论】:

  • 请告诉我们您的尝试。
  • 使用java.time.Month 代替字符串。
  • 嗯,我用 Month 类将它转换为数字,然后将月份对象转换为字符串。然后我创建了一个带有特定月份的字符串形式的随机日期,然后我将它转换为日历类型并增加它。然后我将日历类型转换回字符串,并从中取出月份。最重要的是,它不起作用
  • String nextMon = Month.valueOf(mon.toUpperCase()).plus(1).getDisplayName(TextStyle.FULL, Locale.US);

标签: java string date


【解决方案1】:

正如 Slaw 所说,使用 Month 表示一年中的一个月,而不是字符串。将您的字符串解析为Month,如下所示:

    DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("MMMM", Locale.ENGLISH);

    String monthAsString = "January";

    Month mon = monthFormatter.parse(monthAsString, Month::from);
    System.out.println(mon);

目前的输出是:

一月

增加。只需添加 1:

    mon = mon.plus(1);
    System.out.println(monthFormatter.format(mon));

二月

【讨论】:

    【解决方案2】:

    有很多方法可以做到这一点。这里有一个小概念。这是一种方法,您可以提供全名或有效的 3 字母缩写(忽略字母大小写)月份数字(从 1 到 12)的月份字符串。返回所提供月份的下一个月份。

    如果提供 December,则返回 January

    如果提供 apr,则返回 May

    如果提供 6(六月),则返回 七月

    方法如下:

    /**
     * Returns the next month that will follow the supplied month string. Either a 
     * full month name, a three letter month name abbreviation, or a month numerical 
     * value (from 1 to 12) can be supplied to this method.<br><br>
     * 
     * @param month (Integer OR String) If String then a full month name or three 
     * letter abbreviation can be supplied. Letter case is ignored. If Integer is 
     * supplied then any value from 1 to 12 (inclusive) can be supplied.<br>
     * 
     * @return (String) The full month name that will be next after the supplied 
     * month.
     */
    public static String getNextMonth(Object month) {
        String res = null;
        String[] months = {"January", "February", "March", "April", "May", "June", 
                 "July", "August", "September", "October", "November", "December"};
        // Is Integer supplied...
        if (month instanceof Integer && (int)month >= 1 && (int)month <= 12) {
            // Ternary used here to get desired month from Array
            res = months[(((int)month - 1) == (months.length - 1) ? 0 : (int)month)];
            return res;
        }
        // Is String supplied...
        else if ((month instanceof String && month.toString().trim().equals("") || month.toString().length() < 3 || 
                !Arrays.asList(months).toString().toLowerCase().contains(month.toString().toLowerCase()))) { 
            System.err.println("Invalid Month Supplied!" + System.lineSeparator());
            // Do what you like with this error!
        }
        else {
            for (int i = 0; i < months.length; i++) {
                if (months[i].toLowerCase().startsWith(month.toString().toLowerCase())) {
                    // Ternary used here to get desired month from Array
                    res = months[(i == (months.length - 1) ? 0 : (i + 1))];
                    break;
                }
            }
        }
        return res;
    }
    

    要测试这个方法,你可能会使用这样的东西:

    Scanner input = new Scanner(System.in);
    String ls = System.lineSeparator();
    while (true) {
        System.out.println("Enter Month: ");
        String mon = input.nextLine();
        if (mon.equalsIgnoreCase("q")) { break; }  // Quit
        String nextMon;
        // Is the supplied text a integer numerical value?
        if (mon.matches("\\d+")) {
            // Convert it from string to integer.
            int monthNum = Integer.parseInt(mon);
            // Passing an integer Month value to the 
            // getNextMonth() method.
            nextMon = getNextMonth(monthNum);
        }
        else {
            // Passing a String month name or (3 letter 
            //  abreviation) to the getNextMonth() method.
            nextMon = getNextMonth(mon);
        }
        if (nextMon != null){
            System.out.println(nextMon + ls);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-01
      • 2014-11-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-19
      • 1970-01-01
      • 2022-01-26
      相关资源
      最近更新 更多