繁体   English   中英

变更日期格式

[英]Change date format

我在将日期格式更改为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();

}

输出:[WST 2013年5月28日星期二00:00:00]

有人可以帮我吗,谢谢! :D

我添加了此while语句,然后将日期格式再次设置为默认格式。

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以来的毫秒数

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

您需要使用DateFormatDate实际格式化为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

扩展以满足变更的要求

您在犯同样的错误。 Date是一个容器,用于存储自该纪元以来的毫秒数,它没有自己的格式,而是使用自己的格式。

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();
}

哪个输出...

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

while没有意义。

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

cal2.getTime()返回一个Date ,然后您尝试从中创建一个列表...虽然我可能遗漏了一些...

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
String str4=label.getText();
    Date date=null;
    try {
        date = formatter.parse(str4);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }  
    formatter = new SimpleDateFormat("dd/MMM/yyyy"); 

    System.out.println("Date :" +formatter.format(date));  

检查下面的代码片段

 try {
            SimpleDateFormat sdin = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdout = new SimpleDateFormat("yyyy-MMM-dd");
            Date date = sdin.parse("2013-05-31");
            System.out.println(sdout.format(date));
        } catch (ParseException ex) {
            Logger.getLogger(TestDate.class.getName()).log(Level.SEVERE, null, ex);
        }

我写了一个静态实用程序方法,您可以直接使用它...希望它足够清晰,可以演示SimpleDateFormat parse()和format()方法的正确用法:

  /**
   * Returns a reformatted version of the input date string, where the format 
   * of the input date string is specified by dateStringFormat and the format 
   * of the output date string is specified by outputFormat.  Format strings 
   * use SimpleDateFormat format string conventions.
   *
   * @param dateString input date string
   * @param dateStringFormat format of the input date string (e.g., dd/MM/yyyy)
   * @param outputFormat format of the output date string (e.g., MMM dd, yyyy)
   *
   * @return reformatted date string
   *
   * @throws ParseException if an error occurs while parsing the input date 
   *                        string using the provided format
   *
   * @author Steve
   */
  public static final String reformatDateString(final String dateString,
                                                final String dateStringFormat,
                                                final String outputFormat) 
                                                throws ParseException {

     final SimpleDateFormat dateStringParser = new SimpleDateFormat(dateStringFormat);
     final SimpleDateFormat outputFormatter = new SimpleDateFormat(outputFormat);

     return outputFormatter.format(dateStringParser.parse(dateString));
  }

您将其称为如下:

   System.out.println(reformatDateString("2013-5-28", "yyyy-MM-dd", "dd/MMM/yyyy"));

在此示例中,将输出以下内容:

   28/May/2013

基本思想是,通常将SimpleDateFormat实例用于以下两种情况之一:

  1. 使用parse()方法将包含已知格式的日期的字符串转换为java.util.Date实例...
  2. 使用format()方法将java.util.Date实例转换为指定格式的字符串...

我正在用我在该方法中创建的两个不同的SimpleDateFormat实例编写的方法中的一行执行这两个操作-一个是使用输入格式创建的(用于将原始String解析为Date实例)...另一个是使用输出格式(用于将创建的日期转换回具有所需格式的字符串)。

如果要以某种格式显示日期,则应使用format函数并使用String输出显示,而不要使用日期对象

例如,考虑以下代码:

String pattern = "dd/MM/yyyy";
DateFormat df = new SimpleDateFormat(pattern);
Date d = new Date();
try {
   String outputString = df.format(d);
   System.out.println("outputString :"+outputString);
} catch (ParseException e) {
   e.printStackTrace();
}

parse函数只是解析特定格式的字符串以创建Date对象,但它不会更改任何date对象的显示属性。

System.out.println(format.format(toDate))

这将以要求的格式显示日期。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM