0

I am trying to convert Current Time Stamp into "dd-MMM-yy HH:mm:ss a" format but not getting an expected output.

My Expected output in java.sql.Timestamp = 08-May-20 14:50:43 PM I want to store it in Java.sql.Timestamp variable but getting output in a different format

Note - I tried approx all the solutions on SO. I request you to please suggest whats doing wrong in my code.

I am trying to convert String to java.sql.Timestamp as mention above format.

public class TimeExample {

    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        System.out.println("Before : " + now);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yy HH:mm:ss a");
        String formatDateTime = now.format(formatter);
        System.out.println("formated  --- " + formatDateTime);
        LocalDateTime localDateTime = LocalDateTime.parse(formatDateTime, formatter);
        System.out.println("DD : " + localDateTime);
        Timestamp timeStamp = Timestamp.valueOf(localDateTime);
        System.out.println("timeStamp : " + timeStamp);
    }
}

>**My Expected output in java.sql.Timestamp = 08-May-20 14:50:43 PM**
> output - Before : 2020-05-08T14:50:43.053
> formated  --- 08-May-20 14:50:43 PM
> DD : 2020-05-08T14:50:43
> timeStamp : 2020-05-08 14:50:43.0
Onic Team
  • 1,620
  • 5
  • 26
  • 37
  • I don't have only date for `formated` row , I have also hours and PM, Are your sure of your output ? Strange is your output, the hour is on following line, why is that ? In the end : your code works well for me https://repl.it/repls/KnowledgeableVapidCron – azro May 08 '20 at 09:34
  • @azro Someone edited my question and remove my actual output. Then I re-edit Please check once – Onic Team May 08 '20 at 09:38
  • Not an answer to your strange carriage return issue, but isn't it easier to create a java.sql.Timestamp from the millis derived from the input LocalDateTime? – Chris May 08 '20 at 09:39
  • What is wrong with the timestamp? Are you talking about the missing `PM`? – akuzminykh May 08 '20 at 09:40
  • To get a specified output you need to use a String, **this** would be a fixed format, If you use a Timestamp object and try to print it you'll just get the default formatting of Timestamp, you may differenciate the value (stored in LocalDateTime, Timestamp, whatever) AND a representation of it – azro May 08 '20 at 09:40
  • I want to store it in Java.sql.Timestamp variable but getting output in a different format – Onic Team May 08 '20 at 09:41
  • @akuzminykh : I want to store it in Java.sql.Timestamp variable but getting output in a different format – Onic Team May 08 '20 at 09:42
  • @VedPrakash I think you are mixing up *formatting* and *storage*. Those are two different things. Btw. `HH` is for *hour-of-day*. When you use am/pm with `a` and want the hours then use `hh` that is *clock-hour-of-am-pm*. Check out [`DateTimeFormatter`](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html) for more details. – akuzminykh May 08 '20 at 09:44
  • 1
    YOU HAVE it stored, but just the default formatting of Timestamp is different from the one you want, if you want to show it in your format, format into a String, to get ONE representation – azro May 08 '20 at 09:48
  • @akuzminykh I already tried HH , hh all the possible formate, – Onic Team May 08 '20 at 09:49
  • 1
    I'm still not sure why you are playing with formatted strings if your actual requirement is to convert a LocalDateTime into a java.sql.Timestamp -am I misunderstanding what you are ultimately aiming to do? – Chris May 08 '20 at 17:02
  • I recommend you don’t use `java.sql.Timestamp`. That class is poorly designed and long outdated. Instead just store your `LocalDateTime` directly into your database (if that’s what you thought you needed a `Timestamp` for). – Ole V.V. May 10 '20 at 18:57
  • Does any of the following answer your question? [This](https://stackoverflow.com/questions/19544067/how-to-format-a-java-sql-timestampyyyy-mm-dd-hhmmss-s-to-a-dateyyyy-mm-dd-h) or [this](https://stackoverflow.com/questions/23692117/formatting-timestamp-in-java) or [this](https://stackoverflow.com/questions/57904783/is-there-a-simple-way-to-change-a-timestamp-value-containing-seconds-and-ms-to-a). – Ole V.V. May 10 '20 at 19:02
  • `14:50:43 PM` is a peculiar redundant format. Since the hour is 14, we already know it’s PM. Are you sure you want that? Do you then want `00:50:43 AM` in the first hour after midnight where people usually write `12:50:43 AM`? – Ole V.V. May 10 '20 at 19:05

1 Answers1

1

I think you can get using DateTimeFormatter:

import java.time.format.DateTimeFormatter;  
import java.time.LocalDateTime;    
public class Main
{

    public static void main(String[] args) {

    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd-MMM-yy HH:mm:ss a");  
    LocalDateTime dt = LocalDateTime.now();  
    System.out.println(dtf.format(dt));  

    }
}
Klarth
  • 2,035
  • 14
  • 26