I am getting datetime Default CURRENT_TIMESTAMP result from MSSQL server database, Now i need to parse this date,time etc. Database result is like 2016-05-14 12:54:01.363
All i need is to traverse this datetime format. As i am doing it below.
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try
{
Date date = format.parse(dateStr);
SimpleDateFormat todayFormat = new SimpleDateFormat("dd");
String dateToday = todayFormat.format(date);
format = dateToday.equals(today) ? new SimpleDateFormat("hh:mm a") : new SimpleDateFormat("dd LLL, hh:mm a");
String date1 = format.format(date);
timestamp = date1.toString();
//
}
catch (ParseException e)
{
e.printStackTrace();
}
But I am getting error of:
java.text.ParseException: Unparseable date: "{"date":"2016-05-14 12:54:01.363000","timezone":"UTC","timezone_type":3}" (at offset 0)
at line 3 .i.e Date date = format.parse(dateStr);
Need help!
The problem depends on how you construct the format variable, and it's good practice to use passed-in pattern rather than default one. In your case (the argument is like "2016-05-14 12:54:01.363", then the format could be like that:
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date date = format.parse(dateStr);
If your dataStr is returned by a json web service call with the format as indicated in the exception, then you can map the dateStr to the response type, for instance assumed it is called MyDate class.
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"));
MyDate myDate = mapper.readValue(dateStr, MyDate.class);
Date date = myDate.getDate();
If you don't know the return type, you can simply map it to a Map class:
Map dateMap = new ObjectMapper().readValue(dateStr, Map.class);
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date date = format.parse(dateMap.get("date"));