更改日期格式

我无法将日期格式更改为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]

有人可以帮我这个,谢谢! :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 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 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); } 

如果要以特定格式显示日期,则应使用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对象,但它不会更改任何日期对象显示属性。

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

这将以所需格式显示日期。

我写了一个静态实用程序方法,你可以直接使用…并希望它足够清楚,以certificate正确使用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 (eg, dd/MM/yyyy) * @param outputFormat format of the output date string (eg, 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()方法将包含具有已知格式的日期的String转换为java.util.Date实例,或者
  2. 要使用format()方法将java.util.Date实例转换为指定格式的String …

我在我使用我在该方法中创建的两个不同的SimpleDateFormat实例编写的方法中的一行中进行了两行 – 一个使用输入格式创建(用于将原始String解析为Date实例)…以及使用输出格式(用于将创建的Date转换回具有所需格式的String)。