Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse given string format to dd-MMM-yyyy hh:mm:ss:aa?

I can get the output as Wed May 11 15:36:08 IST 2016, but how do I convert the date to a string with the required format?

Required format is: 12-05-2016 16:05:08 pm

What I tried is,

public class Test {
    public static void main(String args[]) throws ParseException{
        String epoche="1462961108000";
        Long initialLogTime = Long.valueOf(epoche);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(initialLogTime);
        Calendar fromDateTime = calendar;
        Calendar toDateTime = fromDateTime;
        toDateTime.add(Calendar.MINUTE, 30);
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss:aa");
        String datestring = String.valueOf(fromDateTime.getTime());
        String datestring1 = String.valueOf(toDateTime.getTime());
        System.out.println(datestring); //here output is Wed May 11 15:36:08 IST 2016
        System.out.println(datestring1); // here output is Wed May 11 15:36:08 IST 2016
        Date dates = dateFormat.parse(datestring);
        Date date1s = dateFormat.parse(datestring1);
        System.out.println(dates);
        System.out.println(date1s);
    }
}

The error I am getting is:

Exception in thread "main" java.text.ParseException: Unparseable date: "Wed May 11 16:05:08 IST 2016"
at java.text.DateFormat.parse(DateFormat.java:357)
at test.Test.main(Test.java:27)
like image 643
Star Avatar asked May 12 '16 09:05

Star


People also ask

How to parse date from string to DD/MM/YYYY in Java?

How to parse Date from String in the format: dd/MM/yyyy to dd/MM/yyyy in java? The java.text package provides a class named SimpleDateFormat which is used to format and parse dates in required manner (local). One of the constructors of this class accepts a String value representing the desired date format and constructors SimpleDateFormat object.

What is the format of DD/MM/YYYY hh/mm?

user insert in dd/mm/yyyy hh:mm:ss format. yyyy-mm-dd hh:mm:ss.000 format. The content must be between 30 and 50000 characters.

How to extract date format “dd-mmm-yyyy” from a string in JavaScript?

We have a string, and we want to extract the date format “dd-mmm-yyyy” from the string. There is no native format in JavaScript for” dd-mmm-yyyy”.

How to convert simpledateformat to string in Java?

Convert it into java.util.Date object. Instantiate the SimpleDateFormat class by passing the desired (new) format as string to its constructor. Invoke the format () method by passing the above obtained Date object as parameter.


1 Answers

You need to format your dates accordingly. This shall help you

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss:aa");
    System.out.println(dateFormat.format(fromDateTime.getTime()));
    System.out.println(dateFormat.format(toDateTime.getTime()));
like image 139
Sanjeev Avatar answered Sep 28 '22 11:09

Sanjeev