I want to parse this json string into date
"startDateTime":"2014-08-10T20:08:45.0218Z"
and then parse it to another date format.
I thought using this:
Gson gson = new GsonBuilder().setDateFormat("dd/MM HH:mm ???").create;
but I'm not sure how what is the format of "2014-08-10T20:08:45.0218Z"
is it yyyy-mm-dd ???
2014-08-10T20:08:45.0218Z
This looks like an ISO 8601 date
Date and time expressed according to ISO 8601:
Combined date and time in UTC: ... 2014-08-20T19:23:25Z
so
yyyy-MM-dd'T'HH:mm:ss.SSSZ
should do it.
Joda time also provides ISODateTimeFormat since this is a common format.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String date = sdf.format(new Date());
System.out.println(date);
Date d = sdf.parse(date);
Related
When I get the date and time in MySQL it retrieves it in this format:
2016-01-14 14:24:00.0
Where does the .0 come from and how do I get this format with Java:
2016-01-14 14:24:00
You can use 2 ways to do this.
Split the date string at '.'
String date = "2016-01-14 14:24:00.0";
String newDate = date.split("\\.")[0];
System.out.println(newDate);
Use SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date date = sdf.parse("2016-01-14 14:24:00.0");
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));
If you are getting date as string, you can use Format to get the format you need:
SELECT DATE_FORMAT(YourDateField, '%d-%m-%Y %T')
FROM YourTable
Have a look here for all possible formats
If you are getting your date as a java.sql.Timestamp, you can get the corresponding java.util.Date instance very easily and then format it to the desired string representation with a java.text.SimpleDateFormat class.
I want to convert a string representation of date in any format to java.sql.Timestamp,(format is also provided)
we have tried using Timestamp.valueOf() and Date.valueOf() but both these method requires String in a particular format,What i need is a generic method which will convert a given String date in any format to java.sql.Timestamp
Thanks in Advance
If you have the string and know the format you can try something like:
dateStr = "25 December 2015"
date = new SimpleDateFormat("dd MMMM yyyy", Locale.ENGLISH).parse(dateStr);
java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(date.getTimestamp());
Or similar:
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
java.util.Date date = simpleDate.parse("2015-12-31 09:45:52.189");
java.sql.Timestamp = new java.sql.Timestamp(date.getTime());
I want to convert this incoming date "962409600000" to date.I first tried to convert it to datetime format "2000-07-01T05:45:00.000+05:45" and then convert it to 2000-07-01.But I am successful to convert to datetime format.Please help me how ca I do that.
Thanks
First parse the String to Long format
Long inComingDate = Long.parseLong("962409600000");//parse the string to long
Construct the Date object and format it using SimpleDateFormat
Date date = new Date(inComingDate); //Convert the java.util.Date
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy"); //convert it format you desire
String dateInString = format.format(date);
Well to get it as a Date, just use:
Date date = new Date(962409600000L);
If you want that as a string representation, use:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String text = format.format(date);
Note the part setting the time zone to UTC - the value you've given is midnight UTC on July 1st. If you apply the default time zone, you'll end up with the day before anywhere which is behind UTC in summer time.
One of the fields which I extract from a database is a date in string format and I need to convert it into a date type to compare with another date.
How do I do this please? Everything I have tried so far gives me an error of java.util.Date cannot be cast to java.sql.Date and this is attempting the following example;
simpledateformat-gives-java-lang-classcastexception-java-util-date
An example of the date extracted in string format is "2012-10-15 09:00:29.157". I think the format to declare is yyyy-MM-dd HH:mm:ss.SSS but not sure.
Thanks
You can create a java.util.Date from java.sql.Date as follows:
java.util.Date date = new java.util.Date(resultSet.getDate("Column").getTime());
Alternatively you can convert the java.sql.Date to either java.util.Calendar or Joda library's DateTime and perform comparisons.
If you are looking at parsing date, then java.text.SimpleDateFormat is your friend.
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
java.util.Date date = format.parse("2012-10-15 09:00:29.157");
It simply means you are assigning java.util.Date in java.sql.Date
From your example you have to do following :
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
java.util.Date date = (java.util.Date)formatter.parse(""2012-10-15 09:00:29.157"");
Convert to Date as Vikdor showed. then get the long timestamp = date.getTimestamp();
Now this long value is millisecons since 1970 utc. just compare by this value.
I have the following date:
2011-10-07T08:51:52.006Z
Now I want to parse it into a GregorianCalendar. Is there an easier way to do it than using substrings and parsing them to Integers?
And what is the Z in the time string?
I tried to parse it using SimpleDateFormat, but I canĀ“t find a explanation for the T in the date String.
DateFormat format = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" )
Date date = format.parse( "2011-10-07T08:51:52.006Z" );
Calendar calendar = new GregorianCalendar();
calendar.setTime( date );
I would take a look at DateTimeFormatter
DateTimeFormatter formatter =
DateTimeFormat.forPattern("<custom_pattern>").withOffsetParsed();
DateTime dateTime = formatter.parseDateTime("<your_input>");
GregorianCalendar cal = dateTime.toGregorianCalendar();
The T in your string acts as a separator between the date and the time and the Z is the time-zone information both as per ISO-8601 format.
You could use the SimpleDateFormatter to parse the String. Please read the javadoc for the aforementioned class to know what could be the format string. 'Z' indicates the timezone information.