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)
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.
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.
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”.
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.
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()));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With