Java Date To String

Hey guys!! How are you doing?? Welcome to flower brackets blog. In this post we are going to learn java date to string.

java date to string

Types of date formats in java :

Date FormatExample
dd-MM-yyyy HH:mm:ss05/06/05 14:10:05
dd/MM/yyyy05/06/2005
d/M/yy5/6/05
d/M/yyyy5/6/2005
ddMMyy50605
ddMMyyyy5062005
ddMMMyy05Jun05
ddMMMyyyy05Jun2005
dd-MMM-yyyy05-Jun-2005
MM dd, yyyy05 06, 2005
E, MMM dd yyyySat, Jun 05 2005
E, MMM dd yyyy HH:mm:ssSat, Jun 05 2005 18:20:08

In the below java date to string example or program SimpleDateFormat class is used.

Also Read – Date Time Java Program

Here i’m using different date formats and converting it to string. Let’s get into the java convert date to string

Example : java date to string

 

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateToString
{
   public static void main(String[] args)
   {
      DateFormat format1 = new SimpleDateFormat("dd/MM/yyyy");
      DateFormat format2 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
      DateFormat format3 = new SimpleDateFormat("dd-MMM-yyyy");
      DateFormat format4 = new SimpleDateFormat("MM dd, yyyy");
      DateFormat format5 = new SimpleDateFormat("E, MMM dd yyyy");
      DateFormat format6 = new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");
      Date systemDate = new Date();
          
      try
      {
         String checkDate = format1.format(systemDate);           
         System.out.println("dd/MM/yyyy format is : " + checkDate);
         String str2 = format2.format(systemDate);          
         System.out.println("dd-MM-yyyy HH:mm:ss format is : " + str2);
         String str3 = format3.format(systemDate);          
         System.out.println("dd-MMM-yyyy format is : " + str3);
         String str4 = format4.format(systemDate);
         System.out.println("MM dd, yyyy format is : " + str4);
         String str5 = format5.format(systemDate);
         System.out.println("E, MMM dd yyyy format is : " + str5);
         String str6 = format6.format(systemDate);
         System.out.println("E, MMM dd yyyy HH:mm:ss format is : " + str6);
      }
      catch (Exception ex)
      {
         System.out.println(ex);
      }
   }
}

Output:

dd/MM/yyyy format is : 04/11/2015
dd-MM-yyyy HH:mm:ss format is : 04-11-2015 18:20:08
dd-MMM-yyyy format is : 04-Nov-2015
MM dd, yyyy format is : 11 04, 2015
E, MMM dd yyyy format is : Sat, Nov 04 2015
E, MMM dd yyyy HH:mm:ss format is : Sat, Nov 04 2015 18:20:08


Conclusion

So guys this is all about convert date to string in java. I hope you have understood the date to string java program.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

You May Also Like