MattBlack MattBlack - 1 year ago 120
Java Question

String to timestamp Android

I have the following string:

19 July 2016 at 07:00:00 UTC

and am using the below code to convert it to a timestamp. However the console is throwing the following error:

java.text.ParseException: Unparseable date: "19 July 2016 at 07:00:00 UTC" (at offset 2)


String time = todayAt7;

DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
try {

Date date = sdf.parse(time);
System.out.println("Date and Time: " + date);

} catch (Exception e1) {

Essentially I am trying to figure out the timestamp for 'today at 7am', is there a way to do this that I am missing?

Answer Source

In your code DateFormat sdf = new SimpleDateFormat("hh:mm:ss"); is not correct. The string argument should match the format of the date string your are trying to parse

try below

DateFormat sdf = new SimpleDateFormat("dd mmmm yyyy 'at' HH:mm:ss z");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download