【问题标题】:Change from strings of numbers to a string of a date?从数字字符串更改为日期字符串?
【发布时间】:2013-10-11 10:03:43
【问题描述】:

如何输入“1502009”并得到输出“2009 年 1 月 15 日”?

我已经阅读了很多关于 SO 的日期问题,但我仍然发现很难找到实现这种特定格式的最佳和最快的方法。

任何帮助表示赞赏。

编辑:我的示例字符串来自我的 android 应用程序中的对话框日期选择器。用户选择日期月份和年份并返回不同的整数。一月表示为 0。所以在我的应用中,我希望 1502009 表示 2009 年 1 月 15 日。

【问题讨论】:

  • “最好最快的方法”是通过编码
  • @Vallentin 好吧,我尝试过使用 simpledateformat,但它也一直在给我时间。像 00:00:23 等,我不知道如何摆脱它。
  • 仅供参考,1502009 不等于 2009 年 1 月 15 日。如果您打算仅提供 1 位月份,则不要尝试提供超过 9 月 的输入日期。
  • 示例字符串中的一月在哪里? 15 -> 月中的某一天,0 -> ? -> 2009 -> 年。您的问题无法回答,因为给出格式的示例字符串不正确。

标签: java date time


【解决方案1】:
use the following code to convert into the required format

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class dateconversion {
    /**
     * @param args
     */
    public static void main(String[] args) {
        String maxDate = "15012009";
        SimpleDateFormat fromFormat = new SimpleDateFormat("ddMMyyyy");
        SimpleDateFormat toFormat = new SimpleDateFormat("MMMM dd, yyyy");
        Date date = null;
        try {
            date = fromFormat.parse(maxDate);
        } catch (java.text.ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("formated date:-" + toFormat.format(date));
    }
}

【讨论】:

  • "1512009" 给了我formated date:-December 15, 0009,而它应该给我January 15, 2009
  • 我已经完成了两位数月份的代码。如果您想要一位数月份,那么您只需使用以下代码更改代码中的 fromFormat。 SimpleDateFormat fromFormat = new SimpleDateFormat("ddMyyyy");
  • 我修改了你的代码以获得完美的答案,你介意我编辑你的答案然后接受吗?
  • 你确定你能做到。粘土
【解决方案2】:

试试下面的简单代码:

public static void main(String[] args) throws ParseException {
    String str="1502009";
    SimpleDateFormat format=new SimpleDateFormat("DDMyyyy");
    SimpleDateFormat resformat=new SimpleDateFormat("MMMM DD, yyyy");
    Date date =format.parse(str);
    System.out.println(resformat.format(date));
}

【讨论】:

  • 我已经编辑了我的答案。这将适用于 1 位数的月份。如果您需要 2 位数的月份,则需要新的模式。
  • 0009 年 1 月 15 日 是输出。 OP 字符串有问题。您为什么要为此提供解决方案?
  • 如果输入像 15112009 它给出了什么?
  • @Masud 感谢您的回复。它可以工作一位数月,但不能工作 2 位数月。我要努力解决它:)
【解决方案3】:

我建议你取 3 个整数变量并使用 Integer.parseInt 和 substring 计算所有 3 个字段,然后进行映射。 再次计算输出字符串时,您可以添加所有单独的字符串。

我这样说是因为您的输入似乎不正确。

【讨论】:

  • 我可以将其作为最后的手段。我只是想知道是否有更简单的方法。请查看我的编辑以了解我的输入来自哪里。
【解决方案4】:

所以月份可以在 [0-11] 范围内,对吗?

我认为这不是一个好主意。为了方便您解析字符串,您需要为每个字段定义/恒定宽度,以便您可以应用模式ddMMyyyy。否则,您必须找到一种方法来计算每个部分的宽度才能正确解析它,这可能会很棘手。比如你有1112008,是January 11, 2008还是December 1, 2008

我建议您更正日期选择器以返回易于解析的日期字符串表示:

@Test
public void formatDateFromPicker() throws ParseException {
    // values from date picker
    int day = 15;
    int month = 0;
    int year = 2009;

    // build the easy to parse string from date picker: 15012009
    String strDate = String.format("%02d%02d%04d", 
            day, (month+1 /* add 1 if months start at 0 */), year);
    System.out.println(strDate);

    // parse the date string from the date picker
    Date date = new SimpleDateFormat("ddMMyyyy").parse(strDate);

    // ouput January 15, 2009
    System.out.println(new SimpleDateFormat("MMMM dd, yyyy").format(date));
}

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    相关资源
    最近更新 更多