I am new to Java. Postgres db contain date format is yyyy-MM-dd
. I need to convert to dd-MM-yyyy
.
I have tried this, but wrong result is display
public static void main(String[] args) throws ParseException {
String strDate = "2013-02-21";
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date da = (Date)formatter.parse(strDate);
System.out.println("==Date is ==" + da);
String strDateTime = formatter.format(da);
System.out.println("==String date is : " + strDateTime);
}
Use datetime. strftime(format) to convert a datetime object into a string as per the corresponding format . The format codes are standard directives for mentioning in which format you want to represent datetime. For example, the %d-%m-%Y %H:%M:%S codes convert date to dd-mm-yyyy hh:mm:ss format.
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
Date date = format1.parse("2013-02-21");
System.out.println(format2.format(date));
You need to use two DateFormat
instances. One which contains the format of the input string and a second one which contains the desired format for the output string.
public static void main(String[] args) throws ParseException {
String strDate = "2013-02-21";
DateFormat inputFormatter = new SimpleDateFormat("yyyy-MM-dd");
Date da = (Date)inputFormatter.parse(strDate);
System.out.println("==Date is ==" + da);
DateFormat outputFormatter = new SimpleDateFormat("dd-MM-yyyy");
String strDateTime = outputFormatter.format(da);
System.out.println("==String date is : " + strDateTime);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With