Java日期格式化程序

我将日期格式设为“YYYY-mm-dd hh:mm”作为格式化程序对象。

如何格式化输入格式化程序对象以仅获得“YYYY-mm-dd”;

我将日期格式设为“YYYY-mm-dd hh:mm”作为格式化程序对象。 如何格式化输入格式化程序对象以仅获得“YYYY-mm-dd”;

您不能将日期设为YYYY-mm-dd,它应该是yyyy-MM-dd 。 要在yyyy-MM-dd中获取日期,请输入以下代码:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String formattedDate = formatter.format(todaysDate); 
 Format formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm"); Date date; try { date = (Date)((DateFormat) formatter).parse("2011-04-13 05:00"); formatter = new SimpleDateFormat("yyyy-MM-dd"); String s = formatter.format(date); System.out.println(s); } catch (ParseException e) { e.printStackTrace(); } 

使用SimpleDateFormat

 String myDateString = "2009-04-22 15:51"; SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(outFormat.format(inFormat.parse(myDateString))); 

如果您的日期格式为"YYYY-mm-dd hh:mm"并且您希望它为"YYYY-mm-dd"我建议您使用inputDate.substring(0, 10)

无论哪种方式,谨防潜在的Y10k错误 🙂

样本格式化日期为Java中的yyyy-MM-dd

 Format formatter = new SimpleDateFormat("yyyy-MM-dd"); Calendar now = Calendar.getInstance(); System.out.println("Now: "+formatter.format(now.getTime()) ); 

是的,您正在寻找SimpleDateFormat http://dlc.sun.com.edgesuite.net/jdk/jdk-api-localizations/jdk-api-zh-cn/builds/latest/html/en/api/java /text/SimpleDateFormat.html

SimpleDateFormat是您正在寻找的。

尝试这个:

 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String formattedDate = formatter.format(todaysDate); 

使用此代码:

 Date date=new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String formattedDate = formatter.format(date); System.out.println("formatted time==>" + formattedDate); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 

其他答案,例如user2663609的答案是正确的。

作为替代方案,java.util.Date / Calendar课程的第三部分开源替代品Joda-Time包含一个满足您需求的内置格式。

 // © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so. // import org.joda.time.*; // import org.joda.time.format.*; String stringIn = "2011-04-07"; // Returns a formatter for a full date as four digit year, two digit month of year, and two digit day of month (yyyy-MM-dd). DateTimeFormatter formatter = ISODateTimeFormat.date().withZone( DateTimeZone.forID( "Europe/London" ) ).withLocale( Locale.UK ); DateTime dateTime = formatter.parseDateTime( stringIn ).withTimeAtStartOfDay(); String stringOut = formatter.print( dateTime ); 

转储到控制台……

 System.out.println( "dateTime: " + dateTime.toString() ); System.out.println( "stringOut: " + stringOut ); 

跑的时候……

 dateTime: 2011-04-07T00:00:00.000+01:00 stringOut: 2011-04-07 

这个问题有很多好的答案! ,这是另一个更通用的解决方案

 public static String getDateInFormate(String oldFormate , String newFormate , String dateToParse){ //old "yyyy-MM-dd hh:mm" //new yyyy-MM-dd //dateTopars 2011-04-13 05:00 String formatedDate=""; Format formatter = new SimpleDateFormat(); Date date; try { date = (Date)((DateFormat) formatter).parse(dateToParse); formatter = new SimpleDateFormat(newFormate); formatedDate = formatter.format(date); } catch (ParseException e) { e.printStackTrace(); } return formatedDate; } 
  SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); String strDate = entry_date; System.out.println("strDate*************"+strDate); Date date = null; try { date = sdf.parse(strDate); } catch (ParseException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); Date yesterday =subtractDay( date); String requiredDate = df.format(yesterday); System.out.println("110 days before*******************"+requiredDate); public static Date subtractDay(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.DATE, -110);`enter code here` return cal.getTime(); }