How to parse date in simple date format
package
BlackBery;
import
java.text.SimpleDateFormat;
import
java.util.Date;
/**
* formatting a date in java examples
*/
/**
*
@author BlackBerry
*
*/
public
class ParsingDate {
/**
*
@param args 2010-11-12
*/
public static void main(String[] args) {
// TODO Todays days formating
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(" Date for blackberry application : "+s.format(new Date()));
}
Getting the following error while parsing JSON date string to Java Date
Exception in thread "main" com.google.gson.JsonSyntaxException: 15-08-2020 10:10:10
at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:107)
at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:82)
at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:35)
at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.Gson.fromJson(Gson.java:791)
at com.google.gson.Gson.fromJson(Gson.java:757)
at com.google.gson.Gson.fromJson(Gson.java:706)
at com.google.gson.Gson.fromJson(Gson.java:678)
at GsonTest.main(GsonTest.java:23)
Caused by: java.text.ParseException: Unparseable date: "15-08-2020 10:10:10"
at java.base/java.text.DateFormat.parse(DateFormat.java:395)
at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:105)
Solution : -
Player class in java
public class Player {
private String name;
private Date dob;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
}
GSON Test class
public class GsonTest {
/**
*
*
* @param args
*/
public static void main(String[] args) {
String data = "{'name':'MS Dhoni','dob':'15-08-2020 10:10:10'}";
Gson gson = new GsonBuilder()
.setDateFormat("EEE, dd-MM-yyyy hh:mm:ss").create();
Player indianplayers= gson.fromJson(data, Player.class);
System.out.println("Cricket player name : "+emp.getName());
System.out.println("Retirement date :"+emp.getDob());
}
}
Thanking you
Updated on 15 Aug 2020
No comments:
Post a Comment
Your feedback may help others !!!