Java Date Formatter

I get the date format as "YYYY-mm-dd hh: mm" as a formatting object.

How can I format the input formatting object to get only "YYYY-mm-dd";

+7
source share
12 answers

I get the date format as "YYYY-mm-dd hh: mm" as a formatting object. How can I format the input of a formatting object to get only "yyyy-mm-dd";

You cannot have a date as YYYY-mm-dd , it must be yyyy-MM-dd . To get the date in yyyy-MM-dd, the following code:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String formattedDate = formatter.format(todaysDate); 
+21
source
 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(); } 
+4
source

Use 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))); 
+2
source

If you get a date in the format "YYYY-mm-dd hh:mm" and want it to be like "YYYY-mm-dd" , I suggest you just use inputDate.substring(0, 10) .

In any case, beware of potential Y10k errors :)

+2
source

The following sample formate date as yyyy-MM-dd in Java

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

SimpleDateFormat is what you are looking for.

0
source

Try the following:

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

Use this code:

 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"); 
0
source

Other answers, such as one by user2663609 , are correct.

Alternatively, the open source replacement of the third part for the java.util.Date/Calendar classes, Joda-Time , includes a built-in format for your needs.

 // © 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 ); 

Dump for console ...

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

At startup ...

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

This question has so many good answers !!, here comes another general solution

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

All Articles