Thursday, 11 February 2016

Date Formatting in Java Example

Date Formatting in Java Example :
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatting {
public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        Date date=new Date();
        String str_date=df.format(date);
        System.out.println("dd-MM-yyyy: "+str_date);
        df.applyPattern("yyyy-MM-dd");
        System.out.println("yyyy-MM-dd: "+df.format(date));      
     
        System.out.println("*****************");        
        SimpleDateFormat df2 = new SimpleDateFormat("dd-MM-yyyy");
        String str="10-02-2016";
        //Date date=new Date();
        Date date1=null;
try {
date1 = df2.parse(str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
        df2.applyPattern("yyyy-MM-dd");
        System.out.println("yyyy-MM-dd: "+df2.format(date1));
}

}



String to Date :
private Date convertStringToDate(String inputString) throws ParseException
{
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date date = formatter.parse(inputString);

return date;
}

PGSQL date column :

@Temporal(TemporalType.DATE) @Column(name="patient_dob", nullable=false) private Date patientDob;
 

No comments:

Post a Comment