-1

My Input is like this:

20180718140032488266000Z-0600

and the way it should go and persist in database is like this:

21-JUL-18 12.05.25.000000000 AM

Currently I have written code like this :

    public static void main(String[] argv) throws ParseException {
        // SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSSZ");
        String dateInString = "20180718140032488266000Z-0600";
        Timestamp ts = formatDate(dateInString);
        System.out.println(ts);
    }

    private static Timestamp formatDate(String dateString) throws ParseException {
        // dateString = dateString.replaceAll("Z", "0000");
        SimpleDateFormat fmtTimestamp = new SimpleDateFormat("yyyyMMddHHmmssSSS'Z'");
        Date dtTimestamp1 = fmtTimestamp.parse(dateString);
        Timestamp timestamp = new java.sql.Timestamp(dtTimestamp1.getTime());
        return timestamp;
    }

I am getting an output in this format: 2018-07-24 05:38:18.0, my question is how can I get AM/PM also in the output. Please suggest solutions. I will be really grateful.

recnac
  • 3,744
  • 6
  • 24
  • 46
  • 1
    There are tons of duplicates. Use java.util.date – jhamon Jul 31 '18 at 14:02
  • 2
    Possible duplicate of [Java string to date conversion](https://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – jhamon Jul 31 '18 at 14:02
  • my output needs to be persisted in database where the field is of TIMESTAMP datatype and the values currently present looks like this : 21-JUL-18 12.05.25.000000000 AM – biswajit dandapat Jul 31 '18 at 14:04
  • Then use a proper library to execute SQL UPDATE commands with parameterized queries. – jAC Jul 31 '18 at 14:07
  • 1
    To correct way would be persist in a `TIMESTAMP` column (or `DATETIME` in some databases). That type of columns doesn't have a format, and if you use prepared statements, you can set it directly without needing a formatted string. It seems you are confused about the JDBC api, and just looking at the default `toString()` of `java.sql.Timestamp`. – Mark Rotteveel Jul 31 '18 at 14:22

2 Answers2

1

my question is how can I get AM/PM also in the output

Well Java 6 is very old. But You need to check the Documentantion for this SimpleDateFromat With this you will see that the letter a is for am/pm Maker:

a   Am/pm marker    Text    PM

And your format is 21-JUL-18 12.05.25.000000000 AM So in the end for adding this you need to make a new SimpleDateFormat

public static void main(String args[]) throws IOException, ParseException {
        String dateInString = "20180718140032488266000Z-0600";
        Timestamp ts = formatDate(dateInString);
        System.out.println(ts);
        SimpleDateFormat fmtTimestamp = new SimpleDateFormat("dd-MMM-YY hh.mm.s.SSSSS a");
        System.out.println(fmtTimestamp.format(ts));
    }

With this you will get this... But there is better solution for this. Like upgrading to a new version to Java. My humble opinion.

Gatusko
  • 2,503
  • 1
  • 17
  • 25
1

The below code will give you date in '2018-07-31 7:39:09 PM' format.Kindly refer http://tutorials.jenkov.com/java-internationalization/simpledateformat.html#formatting-dates for additional info

import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatExample {
public static void main(String[] args) {
    Date date = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd h:mm:ss a");
    String strDate= formatter.format(date);
    System.out.println(strDate);
}

}

Aniket Kadam
  • 162
  • 5