更改日期格式 [英] Change date format

查看:144
本文介绍了更改日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将日期格式更改为 dd / MMM / yyyy

I'm having trouble changing the date format to dd/MMM/yyyy.

这是我的当前实施:

final String OLD_FORMAT = "yyyy-MM-dd";

final String NEW_FORMAT = "yyyy-MMM-dd";

//Start Date
String str4=label.getText();
java.util.Date toDate = null;

//System.out.println(str4);

//End Date
String str5=lblNewLabel_3.getText();
java.util.Date newDateString = null;

SimpleDateFormat format = new SimpleDateFormat(OLD_FORMAT);

try {

    toDate=format.parse(str4);

} catch (ParseException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

try {
    newDateString=format.parse(str5);
    format.applyLocalizedPattern(NEW_FORMAT);


} catch (ParseException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();

}

输出:[Tue May 28 00:00:00 WST 2013]

output: [Tue May 28 00:00:00 WST 2013]

有人可以帮我这个,谢谢! :D

can someone help me with this, thanks! :D

我添加这个while语句然后日期格式再次设置为默认值..

I add this while statement then the date format sets to default again..

System.out.println("From " + toDate);

System.out.println("To " + newDateString );

Calendar cal2 = Calendar.getInstance();

cal2.setTime(toDate);

System.out.println(toDate);

while (cal2.getTime().before(newDateString)) {
    cal2.add(Calendar.DATE, 1);
    Object datelist=(cal2.getTime());
    List<Object> wordList = Arrays.asList(datelist);  
    System.out.println(wordList);
}


推荐答案

java.util.Date 没有格式。它只是自1970年1月1日00:00:00 GMT以来的毫秒数

java.util.Date does not have a format. It is simply the number of millisecond since January 1, 1970, 00:00:00 GMT

当您执行 System.out.println时( new Date())它只是提供 Date 对象的默认 toString 方法输出。

When you do a System.out.println(new Date()) it is simply providing the Date objects default toString methods output.

您需要使用 DateFormat 来实际格式化日期字符串

You need to use a DateFormat to actually format the Date to a String

public class TestDate01 {

    public static final String OLD_FORMAT = "yyyy-MM-dd";
    public static final String NEW_FORMAT = "yyyy-MMM-dd";

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            String oldValue = "2013-05-29";
            Date date = new SimpleDateFormat(OLD_FORMAT).parse(oldValue);
            String newValue = new SimpleDateFormat(NEW_FORMAT).format(date);
            System.out.println("oldValue = " + oldValue + "; date = " + date + "; newValue = " + newValue);
        } catch (ParseException exp) {
            exp.printStackTrace();
        }
    }
}

哪些输出...

oldValue = 2013-05-29; date = Wed May 29 00:00:00 EST 2013; newValue = 2013-May-29



扩展以满足更改的要求



你犯了同样的错误。 日期是自纪元以来毫秒数的容器,它没有自己的任何格式,而是使用自己的格式。

Extended to meet changed requirements

You're making the same mistake. Date is a container for the number of milliseconds since the epoch, it does not have any format of it's own and instead uses its own format.

try {
    Date toDate = new Date();
    String newDateString = "2013-05-31";

    System.out.println("From " + toDate);
    System.out.println("To " + newDateString);

    Date endDate = new SimpleDateFormat(OLD_FORMAT).parse(newDateString);

    System.out.println("endDate " + endDate);

    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(toDate);
    System.out.println(toDate);

    SimpleDateFormat newFormat = new SimpleDateFormat(NEW_FORMAT);

    while (cal2.getTime().before(endDate)) {
        cal2.add(Calendar.DATE, 1);
        Date date = (cal2.getTime());
        System.out.println(date + "/" + newFormat.format(date));
    }
} catch (Exception exp) {
    exp.printStackTrace();
}

哪些输出......

Which outputs...

From Wed May 29 15:56:48 EST 2013
To 2013-05-31
endDate Fri May 31 00:00:00 EST 2013
Wed May 29 15:56:48 EST 2013
Thu May 30 15:56:48 EST 2013/2013-May-30
Fri May 31 15:56:48 EST 2013/2013-May-31

没有感觉。

Object datelist=(cal2.getTime());
List<Object> wordList = Arrays.asList(datelist);

cal2.getTime()返回日期,然后你尝试从中创建一个列表......也许我错过了一些东西......

cal2.getTime() returns a Date, then you try and create a list from it...maybe I'm missing something though...

这篇关于更改日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆