Reputation: 707
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)
Upvotes: 5
Views: 41613
Reputation: 79540
java.time
In March 2014, Java 8 introduced the modern, java.time
date-time API which supplanted the error-prone legacy java.util
date-time API. Any new code should use the java.time
API.
Instant
using
Instant#ofEpochMilli
.Instant
into a ZonedDateTime
using the required ZoneId
.ZonedDateTime#plusMinutes
to add minutes to the obtained ZonedDateTime
.DateTimeFormatter
with the required pattern. Normally am/pm marker makes sense with clock-hour-of-am-pm (1-12). The symbol for clock-hour-of-am-pm (1-12) is h
instead of H
, which is used for hour-of-day (0-23). Nevertheless, you can display the am/pm marker with an hour-of-day (0-23) using a
in the pattern. Check documentation to learn more about these symbols.Demo:
public class Main {
public static void main(String[] args) {
String epoche = "1462961108000";
Instant instant = Instant.ofEpochMilli(Long.valueOf(epoche));
// Replace ZoneId.of("Asia/Kolkata") as required e.g.
// to ZoneId.systemDefault() if your requirement is to
// get the date-time in your JVM's time zone
ZonedDateTime zdt = instant.atZone(ZoneId.of("Asia/Kolkata"))
.plusMinutes(30);
String dateString = zdt.format(
DateTimeFormatter.ofPattern("dd-MM-uuuu HH:mm:ss a", Locale.UK));
System.out.println(dateString);
}
}
Output:
11-05-2016 16:05:08 pm
Learn more about the modern Date-Time API from Trail: Date Time.
Upvotes: 2
Reputation: 599
In Android, Pass String Like 11/10/2017 11:16:46 to function ConvertDateTime
public String ConvertUpdate(String strDate) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
try {
Date d = simpleDateFormat.parse(strDate);
simpleDateFormat = new SimpleDateFormat("dd MMM yy hh:mm a");
return simpleDateFormat.format(d);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
output 10 Nov 17 11:16 AM
Upvotes: 0
Reputation: 9946
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()));
Upvotes: 9
Reputation: 26077
Please try this
public static void main(String[] args) {
String epoche = "1462961108000";
Date date = new Date(Long.parseLong(epoche));
DateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss:aa");
String strDate = sdf.format(date);
System.out.println(strDate);
}
Output
11-05-2016 15:35:08:PM
Upvotes: 0
Reputation: 223
if you are using java 8, you can use
LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss:aa");
System.out.println(date.format(formatter));
Upvotes: 0