So I'm using the google blogger.com api and I have a blog post date in Google's Date Time class format.....
http://code.google.com/apis/gdata/javadoc/com/google/gdata/data/DateTime.html
What is the easiest way in Java to format that DateTime object to display in this format?
Mar 1, 2012 12:45 PM
use the SimpleDateFormat class for format the any date format
Related
This question already has answers here:
display Java.util.Date in a specific format
(11 answers)
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
Closed 2 years ago.
Im getting the following error when I try to convert the following string. Id like the Date to be in the format yyyy-MM-dd'T'HH:mm:ss:SSS but instead the Date seems to be coming out as Sun Mar 01 23:00:01 GMT 2020
String FULL_ISO_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
SimpleDateFormat formatter = new SimpleDateFormat(FULL_ISO_DATE_FORMAT);
Date from = formatter.parse("2020-03-01T23:00:01.000");
Error
feign.FeignException: status 400 reading Controller#searchController(Date,Date,Integer,String); content:
{"status":"fail","data":{"errors":[{"type":"IllegalArgumentException","description":"Invalid value Sun Mar 01 23:00:01 GMT 2020 for filter from. Field needs to be in format: yyyy-MM-dd'T'HH:mm:ss.SSS"}]}]}
Any help would be appreciated. I need to use the Date object as the constructor Im querying is using the Date object. Ideally I'd like to use LocalDateTime but I cant.
Use the LocalDateTime from java-8 date-time API and stop using legacy Date classes
String FULL_ISO_DATE_FORMAT = DateTimeFormatter. ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime dateTime = LocalDateTime.now();
dateTime.format(FULL_ISO_DATE_FORMAT);
Please don't use the old classes Date and SimpleDateFormat. Use the new java.time api that is much more robust and better designed.
You can do the same thing as follows:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime date = LocalDateTime.parse("2020-03-01T23:00:01.000", formatter);
Keep in mind that you can convert it to Date for compatibility like so:
Date legacyDate = Date.from(date.atZone(ZoneId.systemDefault()).toInstant());
Server side code (server timezone is UTC):-
Date aDate = new Date();
java.sql.Timestamp aTimestamp = new java.sql.Timestamp(aDate.getTime());
Client side (Mobile app, timezone GMT +5:30):-
Hitting a service request which runs above code on server side
The issue is when i debugged on server, found following values :-
aDate.getTime() prints to -> 1470472883877 milliseconds i.e., Sat Aug 06 2016 14:11:23 GMT+0530
but
aTimestamp prints to -> (java.sql.Timestamp) 2016-08-06 08:41:44.109
It's kinda weird, i've no idea what's going on in conversion !! please help
UTC and GMT are formats.
java.util.Date and java.sql.Timestamp are independent of the timezone. They store a long time in ms for representing their inner state.
For information, Calendar is timezone aware.
So with Date or Timestamp, to differentiate GMT or UTC format in an output, you have to use a formater which outputs the date into string by being aware the timezone.
In your output : 2016-08-06 08:41:44.109, you don't use a formater which is aware of the timezone. It's probably the result of a toString() on the java.sql.Timestamp instance or something of similar.
What you consider as a conversion is not a conversion but a formatting since the timestamp stays the same between the two objects.
If you want to display in the UTC format, use the appropriate formater with a
SimpleDateFormat for example :
SimpleDateFormat dt= new SimpleDateFormat("MM/dd/yyyy hh:mm:ss z");
dt.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateStringInUTC = dt.format(new Date(yourSqlTimestamp.getTime()));
The following is probably what you are looking for:
Calendar cal = Calendar.getInstance(Locale.GERMANY); // use your locale here
Timestamp aTimestamp = new Timestamp(cal.getTimeInMillis());
System.out.println(aTimestamp);
System.out.println(cal.getTime());
And the output:
2016-08-06 19:12:54.613
Sat Aug 06 19:12:54 CEST 2016
Is there a date formatting tool for Atom Dates.
According to this link:
https://www.rfc-editor.org/rfc/rfc4287
Such date values happen to be compatible with the following
specifications: [ISO.8601.1988], [W3C.NOTE-datetime-19980827], and
[W3C.REC-xmlschema-2-20041028].
Example Date constructs:
<updated>2003-12-13T18:30:02Z</updated>
<updated>2003-12-13T18:30:02.25Z</updated>
<updated>2003-12-13T18:30:02+01:00</updated>
<updated>2003-12-13T18:30:02.25+01:00</updated>
I tried to use Joda ISODateTimeFormat.dateTime(); but it seems it doesn't handle the parsing when there is no milliseconds (2003-12-13T18:30:02Z for exemple).
What is the simplest way to parse all these date formats?
This is ISO 8601 format, the standard format used in for example XML. Joda Time supports this format very well, you can just pass these strings to the constructor of DateTime:
DateTime timestamp = new DateTime("2003-12-13T18:30:02Z");
Works without any problems, also if there are no milliseconds in the string.
It seems to be xml dateTime. Then the best choice is javax.xml.datatype.XMLGregorianCalendar.
DatatypeFactory f = DatatypeFactory.newInstance();
XMLGregorianCalendar xgc = f.newXMLGregorianCalendar("2003-12-13T18:30:02.25Z");
System.out.println(xgc);
System.out.println(xgc.toGregorianCalendar().getTime());
output
2003-12-13T18:30:02.25Z
Sat Dec 13 20:30:02 EET 2003
See more in API
DateUtils from Apache Commons / Lang has a parseDate method that supports multiple patterns. That may work for you. (The patterns must be formatted according to the SimpleDateFormat syntax)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Timezone conversion
I have to convert given time and timezone with some other timezone using Java code.
For Example :
I have to convert 28 Sept 2:00 PM IST in Canada timezone with considering DST (Day Light Saving Time) as well.
Can someone help me?
Try getTimeZone() and setTimeZone() along with Calendar class
TimeZone oztz = TimeZone.getTimeZone("Canada/Atlantic");
Calendar datetime = Calendar.getInstance( oztz );
See this link for all Time-Zones :
http://snipplr.com/view/23131/timezone-enum/
Probably you are looking for something like this :
Converting Times Between Time Zones
The Calendar class has built in methods for doing the conversion.
You can set the desired timezone with setTimeZone.
Also see the getTimeZoneOffset from the Date class.
You could use JodaTime and its support for different timezones.
Namely, if you know you have string ISO format or a specific known format (like it looks like), you could do something like:
DateTimeFormatter fmt = ISODateTimeFormat.dateTime().withZone("Asia/Kolkata");
DateTime newDate = fmt.parseDateTime([your input date]);
// You can manipulate your date here...
String newString = fmt.withZone("Canada/Atlantic").print();
This handles internally all Timezone matters, included DST.
Simple way is set Datetime and Timezone to Calender and getTime. Below is sample code which will set Timezone and user specific time.
Date date =new Date(2012,9,28,2,00,00); //Set time to Date
Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("Canada/Atlantic"));
calendar.setTime(date);
System.out.println("Timezone :: " + calendar.getTimeZone());
System.out.println("Time :: " + calendar.getTime());
Output:
Timezone :: sun.util.calendar.ZoneInfo[id="Canada/Atlantic",offset=-14400000,dstSavings=3600000,useDaylight=true,transitions=228,lastRule=java.util.SimpleTimeZone[id=Canada/Atlantic,offset=-14400000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=3,startDay=1,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]
Time :: Mon Oct 28 02:00:00 GMT 3912
You will get all information from TimeZone useDaylight(true/false), dstSaving(1 hour for canada)
For more functionality of TimeZone please refer below link:
http://biese.wordpress.com/2006/10/23/java-daylight-saving-time-and-time-zone/
Here is link to get DayLightSavingTime information:
http://timeanddate.com/worldclock/clockchange.html?n=1187&year=2012
I'm using Flex+java+Mysql.. in that i want to insert the datetime taking from flex to mysql in that i got date String like this
Sat Aug 4 12:05:00 GMT+0530 2012
how to convert yyyy-MM-dd HH:mm:ss z
this formate so please give code in java or flex...
In flex API Given DateFile class, in that we have two methods dateTOString(), stringTODate().
DateField.dateToString(dateInput.selectedDate,"DD-MM-YYYY");
Please Refere Below Doc.
DateField
DateField.dateToString(addr1.selectedDate,"YYYY-MM-DD").toString();