Vyx
Vyx

Reputation: 31

Format timestamp from database to Java

I have this weird format of date and time on my Oracle SQL Developer :

2015-4-14.1.39. 33. 870000000

I tried to format the given date to MM-dd-yyyy HH:mm:ss, but it gives me exception:

java.text.ParseException: Unparseable date: "2015-4-14.1.39. 33. 870000000"

The following is the code:

    SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");

    try {
        String ts = "2015-4-14.1.39. 33. 870000000";
        Date date = formatter.parse(ts);
        String S = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(date);

        System.out.println(S);
    } catch (Exception e) {
        e.printStackTrace();
    }

Upvotes: 0

Views: 229

Answers (1)

ntalbs
ntalbs

Reputation: 29468

The problem is that the given date string does not match the specified format. Try use the following format:

SimpleDateFormat df = new SimpleDateFormat("yyyy-M-dd.H.m. s. S");
String ts = "2015-4-14.1.39. 33. 870000000";
df.parse(ts);

Where

  • yyyy for year
  • M for month in year
  • dd for day in month
  • H for hour in date (0-23)
  • m for minute in hour
  • s for second in minute
  • S for millisecond

Upvotes: 1

Related Questions