如何使用SimpleDateFormat将string转换为date?

我有这个代码片段:

DateFormat formatter1; formatter1 = new SimpleDateFormat("mm/DD/yyyy"); System.out.println((Date)formatter1.parse("08/16/2011")); 

当我运行这个,我得到这个作为输出:

 Sun Jan 16 00:10:00 IST 2011 

我期望:

 Tue Aug 16 "Whatever Time" IST 2011 

我的意思是说我没有像预期的那样得到这个月份。 什么错误?

尝试这个:

 new SimpleDateFormat("MM/dd/yyyy") 
  • MM是“月”(而不是mm
  • dd是“日”(不是DD

这一切都在SimpleDateFormat的javadoc中

仅供参考,您的格式仍然是有效的date格式的原因是:

  • mm是“分钟”
  • DD是“日复一日”

此外,你不需要演员Date …它已经一个Date (或爆炸):

 public static void main(String[] args) throws ParseException { System.out.println(new SimpleDateFormat("MM/dd/yyyy").parse("08/16/2011")); } 

输出:

 Tue Aug 16 00:00:00 EST 2011 

瞧!

米 – 分钟M – 月

 Letter Date or Time Component Presentation Examples G Era designator Text AD y Year Year 1996; 96 M Month in year Month July; Jul; 07 w Week in year Number 27 W Week in month Number 2 D Day in year Number 189 d Day in month Number 10 F Day of week in month Number 2 E Day in week Text Tuesday; Tue a Am/pm marker Text PM H Hour in day (0-23) Number 0 k Hour in day (1-24) Number 24 K Hour in am/pm (0-11) Number 0 h Hour in am/pm (1-12) Number 12 m Minute in hour Number 30 s Second in minute Number 55 S Millisecond Number 978 z Time zone General time zone Pacific Standard Time; PST; GMT-08:00 Z Time zone RFC 822 time zone -0800 

使用下面的function

 /** * Format a time from a given format to given target format * * @param inputFormat * @param inputTimeStamp * @param outputFormat * @return * @throws ParseException */ private static String TimeStampConverter(final String inputFormat, String inputTimeStamp, final String outputFormat) throws ParseException { return new SimpleDateFormat(outputFormat).format(new SimpleDateFormat( inputFormat).parse(inputTimeStamp)); } 

样本用法如下:

  try { String inputTimeStamp = "08/16/2011"; final String inputFormat = "MM/dd/yyyy"; final String outputFormat = "EEE MMM dd HH:mm:ss z yyyy"; System.out.println(TimeStampConverter(inputFormat, inputTimeStamp, outputFormat)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } 
 String newstr = "08/16/2011"; SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy"); SimpleDateFormat format = new SimpleDateFormat("EE MMM dd hh:mm:ss z yyyy"); Calendar c = Calendar.getInstance(); c.setTime(format1.parse(newstr)); System.out.println(format.format(c.getTime())); 

非常简单的例子是。

 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy"); Date date = new Date(); Date date1 = new Date(); try { System.out.println("Date1: "+date1); System.out.println("date" + date); date = simpleDateFormat.parse("01-01-2013"); date1 = simpleDateFormat.parse("06-15-2013"); System.out.println("Date1 is:"+date1); System.out.println("date" + date); } catch (Exception e) { System.out.println(e.getMessage()); } 

使用,

 SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy"); 

代替,

 SimpleDateFormat format1 = new SimpleDateFormat("mm/DD/yyyy"); 

因为,

 mm - is the key to minutes DD - is the key to day in Year 

你可以很简单地解决这个问题,首先将给定的string转换为date对象,例如:

 java.util.Date date1 = new Date("11/19/2015"); SimpleDateFormat formatter = new SimpleDateFormat("MMM dd yyyy HH:mma"); String format = formatter.format(date); System.out.println(format); 

这段代码有助于来回转换

  System.out.println("Date: "+ String.valueOf(new Date())); SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String stringdate = dt.format(new Date()); System.out.println("String.valueOf(date): "+stringdate); try { Date date = dt.parse(stringdate); System.out.println("parse date: "+ String.valueOf(date)); } catch (ParseException e) { e.printStackTrace(); }