Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date Format JAVA

I want to format 2012-05-04 00:00:00.0 to 04-MAY-2012. i have tried it with below steps.

    SimpleDateFormat sdf = new SimpleDateFormat(
            "yyyy-MM-dd 'T' HH:mm:ss.SSS");

    Date date;
    String dateformat = "";
    try {
        date = sdf.parse("2012-05-04 00:00:00.0");
        sdf.applyPattern("DD-MON-RR");
        dateformat = sdf.format(date);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

but i got below exception.

java.text.ParseException: Unparseable date: "2012-05-04 00:00:00.0"
    at java.text.DateFormat.parse(DateFormat.java:337)
    at com.am.test.Commit.main(Example.java:33)`

How could i do this?

like image 624
Bishan Avatar asked May 04 '12 06:05

Bishan


2 Answers

Here, this works:

  1. Remove the extra 'T' in your first pattern
  2. The second format is incorrect, it should be dd-MMM-yyyy.

Take a look at the Javadoc of SimpleDateFormat

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

public class temp2 {

    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

        Date date;
        String dateformat = "";
        try {
            date = sdf.parse("2012-05-04 00:00:00.0");
            sdf.applyPattern("dd-MMM-yyyy");
            dateformat = sdf.format(date);
            System.err.println(dateformat);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
like image 57
Guillaume Polet Avatar answered Sep 21 '22 07:09

Guillaume Polet


I think if you remove the 'T' it will work.

like image 37
Francis Upton IV Avatar answered Sep 24 '22 07:09

Francis Upton IV