MattBlack MattBlack - 4 months ago 16
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)


CODE:

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) {
e1.printStackTrace();
}


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

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");