【问题标题】:Convert Long date to specific short date format将长日期转换为特定的短日期格式
【发布时间】:2013-03-29 05:37:27
【问题描述】:

将长日期格式转换为特定的短日期格式。

我想从 Datepicker(Jcalander) 获取日期,格式为 dd-mm-yyyy 格式并分配给 String 变量。我尝试使用下面显示的代码。但没有得到我想要的日期格式。

SimpleDateFormat simpleFormat = (SimpleDateFormat) jCalendarCombo1.getDateFormat();
Date date = jCalendarCombo1.getDate();
System.out.println(date); // Prints Thu Mar 28 00:00:00 IST 2013


String s = simpleFormat.format(date);
System.out.println(s); // prints Thursday, March 28, 2013

System.out.println("Date SHORT format: "  + DateFormat.getDateInstance(DateFormat.SHORT).format(date));  // prints 3/28/13

【问题讨论】:

    标签: date datepicker long-integer short


    【解决方案1】:

    如果你想要一个固定的,不依赖于语言环境的格式,你可以自己创建它;

    SimpleDateFormat shortformat = new SimpleDateFormat("dd-MM-yyyy");
    String s = shortformat.format(date);
    System.out.println(s);                        // Prints 29-03-2013
    

    【讨论】:

    • 感谢您的快速响应。现在可以了。我得到了 29-03-2013 格式的日期。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多