Thursday, July 21, 2016

Merubah Nilai Tanggal String Menjadi Date

Berikut contoh program merubah nilai tanggal yang masih merupakan String kita rubah menjadi tanggal yang merupakan Date dalam Java. Nam... thumbnail 1 summary

Berikut contoh program merubah nilai tanggal yang masih merupakan String kita rubah menjadi tanggal yang merupakan Date dalam Java.

Nama file : string_To_Date.java

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class string_To_Date {
   public static void main(String args[])
   {
       String testDateString1 = "11/07/2016";
       String testDateString2 = "11-07-2016 23:37:50";
       String testDateString3 = "11-Jul-2016";
       String testDateString4 = "07 11, 2016";
       String testDateString5 = "Thu, Jul 11 2016";
       String testDateString6 = "Thu, Jul 11 2016 23:37:50";
       DateFormat df1 = new SimpleDateFormat("dd/MM/yyyy");
       DateFormat df2 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
       DateFormat df3 = new SimpleDateFormat("dd-MMM-yyyy");
       DateFormat df4 = new SimpleDateFormat("MM dd, yyyy");
       DateFormat df5 = new SimpleDateFormat("E, MMM dd yyyy");
       DateFormat df6 = new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");
       try
       {
           //format() method Formats a Date into a date/time string. 
           Date d1 = df1.parse(testDateString1);
           System.out.println("Date: " + d1);
           System.out.println("Date in dd/MM/yyyy format is: "+df1.format(d1));
           System.out.println("");
           Date d2 = df2.parse(testDateString2);
           System.out.println("Date: " + d2);
           System.out.println("Date in dd-MM-yyyy HH:mm:ss format is: "+df2.format(d2));
           System.out.println("");
           Date d3 = df3.parse(testDateString3);
           System.out.println("Date: " + d3);
           System.out.println("Date in dd-MMM-yyyy format is: "+df3.format(d3));
           System.out.println("");
           Date d4 = df4.parse(testDateString4);
           System.out.println("Date: " + d4);
           System.out.println("Date in MM dd, yyyy format is: "+df4.format(d4));
           System.out.println("");
           Date d5 = df5.parse(testDateString5);
           System.out.println("Date: " + d5);
           System.out.println("Date in E, MMM dd yyyy format is: "+df5.format(d5));
           System.out.println("");
           Date d6 = df6.parse(testDateString6);
           System.out.println("Date: " + d6);
           System.out.println("Date in E, E, MMM dd yyyy HH:mm:ss format is: "+df6.format(d6));

       }
       catch (Exception ex ){
          System.out.println(ex);
       }
   }
}  

Output

run:
Date: Mon Jul 11 00:00:00 ICT 2016
Date in dd/MM/yyyy format is: 11/07/2016

Date: Mon Jul 11 23:37:50 ICT 2016
Date in dd-MM-yyyy HH:mm:ss format is: 11-07-2016 23:37:50

Date: Mon Jul 11 00:00:00 ICT 2016
Date in dd-MMM-yyyy format is: 11-Jul-2016

Date: Mon Jul 11 00:00:00 ICT 2016
Date in MM dd, yyyy format is: 07 11, 2016

Date: Mon Jul 11 00:00:00 ICT 2016
Date in E, MMM dd yyyy format is: Mon, Jul 11 2016

Date: Mon Jul 11 23:37:50 ICT 2016
Date in E, E, MMM dd yyyy HH:mm:ss format is: Mon, Jul 11 2016 23:37:50
BUILD SUCCESSFUL (total time: 2 seconds)

Selamat mencoba, semoga bermanfaat ...

No comments

Post a Comment