Using SimpleDateFormat to convert Date to String - use dd-MM-yyyy and dd-MM-yyyy hh:mm:ss format in java


Contents of page :
  • Program 1) Using SimpleDateFormat to convert Date to String (use dd-MM-yyyy format)
  • Program 2) Using SimpleDateFormat to convert Date to String (use dd-MM-yyyy hh:mm:ss format)

Program 1) Using SimpleDateFormat to convert Date to String (use dd-MM-yyyy format)



import java.text.SimpleDateFormat;
import java.util.Date;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... args) {
          SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
          String stringDate = sdf.format(new Date());
          System.out.println("stringDate = " + stringDate);
   }
}
/*OUTPUT
stringDate = 08-07-2015
*/


for complete list format and combinations that could be used

Program 2) Using SimpleDateFormat to convert Date to String (use dd-MM-yyyy hh:mm:ss format)

import java.text.SimpleDateFormat;
import java.util.Date;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... args) {
          SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
          String stringDate = sdf.format(new Date());
          System.out.println("stringDate = " + stringDate);
   }
}
/*OUTPUT
stringDate = 08-07-2015 04:45:38
*/



RELATED LINKS>
Creating Date >

Creating Date in java using Calendar, GregorianCalendar and java.util.Date | Getting Year, Month, Date, Hour, Minute, Second from current date


Convert Calendar and GregorianCalendar to Date | Convert Date to Calendar and GregorianCalendar in java




Difference between two Dates>

Difference between two dates in days, hours, minutes, seconds, milliSeconds- where dates are manually created using dd-MM-yyyy and dd-MM-yyyy hh:mm:ss format


Check whether a given date lies between other two dates


2 approaches to check whether difference between given two dates is greater than specified number of days or not


eEdit
Must read for you :