halida's

my life, my love, and my journey

Convert Date in Java November 17, 2008

Filed under: Java — halida ernita @ 7:56 AM
Tags:

Ini adalah contoh untuk mengubah tanggal bertipe String menjadi date

import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 /**
 *
 * @author Halida Ernita
 */
 public class DateConverter {
 static java.text.DateFormat  formatter = new SimpleDateFormat("dd/MM/yyyy");
 static java.text.DateFormat sqlDateFormatter=new SimpleDateFormat("yyyy-MM-dd");

public static void main(String args[]){
 //String Date in format dd/MM/yyyy
 String stringDate="28/12/2008";
 //convert from String to Date
 Date utilDate=convertStringToDate(stringDate);
 System.out.println("Convert Date From String="+stringDate+" to Date="+utilDate);
 //convert from String to SQLDate
 java.sql.Date sqlDate=convertStringToSqlDate(stringDate);
 System.out.println("Convert Date From String="+stringDate+" to sql Date="+sqlDate);
//String Date in format yyyy-MM-dd
 String stringSqlDate="2008-12-28";
 //convert from String to SQLDate
 java.sql.Date sqlDate2=java.sql.Date.valueOf(stringSqlDate);
 System.out.println("Convert Date From String="+stringSqlDate+" to sql Date="+sqlDate2);
 //convert from String to Date
 Date utilDate2=convertStringSqlToDate(stringSqlDate);
 System.out.println("Convert Date From String="+stringSqlDate+" to Date="+utilDate2);
 //convert from String in format yyyy-MM-dd to dd/MM/yyyy
 String dateUtilFormat=convertStringSqlToStringDate(stringSqlDate);
 System.out.println("Convert Date From String="+stringSqlDate+" to String in diff format="+dateUtilFormat);

}

//Method that convert String date from dd/MM/yyyy to Utils date
 public static Date convertStringToDate(String stringDate){
 try {
 java.util.Date utilDate = formatter.parse(stringDate);
 return utilDate;
 } catch (ParseException ex) {
 Logger.getLogger(DateConverter.class.getName()).log(Level.SEVERE, null, ex);
 }
 return null;
 }

//Method that convert String date from dd/MM/yyyy to sql Date
 public static java.sql.Date convertStringToSqlDate(String stringDate){

//first convert string Date to Util Date then convert to sqlFormat
 Date utilDate=convertStringToDate(stringDate);
 String stringDateSqlFormat=sqlDateFormatter.format(utilDate);
 java.sql.Date sqlDate=java.sql.Date.valueOf(stringDateSqlFormat);
 return sqlDate;
 }

//Method that convert String date from yyyy-MM-dd to Utils date
 public static Date convertStringSqlToDate(String stringSqlDate){
 try {
 java.util.Date utilDate = sqlDateFormatter.parse(stringSqlDate);
 return utilDate;
 } catch (ParseException ex) {
 Logger.getLogger(DateConverter.class.getName()).log(Level.SEVERE, null, ex);
 }
 return null;
 }

//Method that convert String date from yyyy-MM-dd to Utils date
 public static String convertStringSqlToStringDate(String stringSqlDate){
 try {
 java.util.Date utilDate = sqlDateFormatter.parse(stringSqlDate);
 String stringDateFormat=formatter.format(utilDate);
 return stringDateFormat;
 } catch (ParseException ex) {
 Logger.getLogger(DateConverter.class.getName()).log(Level.SEVERE, null, ex);
 }
 return null;
 }

}
 

7 Responses to “Convert Date in Java”

  1. Santen Says:

    Sip-sip bener-bener bermanfaat…smg bertambah terus ilmunya dan bermanfaat…

  2. sagoeleuser5 Says:

    ampuuuuun da’…ampuuuuuuun

  3. assalamualaikum warah matulloh wabarakatuh…
    pakabar teh??

    ^^

  4. ridha Says:

    dahsyat… ampun kk.. -_-

  5. Henri H. Says:

    numpang comment ah😀
    1. syntaxnya pake tag sourcecode ya biar enak dibaca..
    kan di wordpress bs pake ini:

         code...
    

    2. buat DateFormat knapa nda pake getDateInstance ajah?? baru pake method applyPattern..

    3. ntu kayanya format “yyyy-MM-dd” specific ke mySql ajah..gmn klo pake rdbms yg beda??..ntu salah 1 alasan gw slalu pake orm hehehe…

  6. halida ernita Says:

    @henry: sip2 hen, thanks sarannya. Tapi perasaan dl pernah dicoba nggak bisa ya di wp pake sintaks itu, atau emang itu baru ada di wp?

  7. bimmariobroz Says:

    nice stuck di date ee ktemu disini tqq


Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s