JodaTime String yyyy-mm-ddThh:mmss.Z to DateTime - java

hi i am using Joda time to convert my string dates to DateTime objects.
I currently have the following string:
2014-02-16T00:17:20.000Z
how do i convert this to a DateTime object?
I have tried:
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZZZ");
DateTime dt = formatter.parseDateTime("2014-02-16T00:17:20.000Z");
But i am getting the following error:
java.lang.IllegalArgumentException: Invalid format: "2014-02-16T00:17:20.000Z" is malformed at ".000Z"
Any help is greatly appreciated

For future visitors, simpler solution:
String date = "2014-02-16T00:17:20.000Z";
DateTime dateTime = new DateTime(date);

This format happens to be the ISO date time format, that DateTime uses by default. You just need
DateTime d = DateTime.parse(s);
or
DateTime d = DateTime.parse(s, ISODateTimeFormat.dateTimeParser());

Might be issue is you guys using Z(zone) in caps
i have tested below code works well
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz", Locale.ENGLISH);
Date date =formatter.parse("2016-09-06T08:35:02.530GMT");
DateTime d = new DateTime(date.getTime());

Related

Format String yyyy-mm-dd hh:mm:ss +timezone into DateTime

I need to format a String that looks like this:
"2018-07-20 18:53:46.598000 +02:00:00"
into a DateTime object like this:
20/07/2018 (HH with Timezone applied):53:46
My approach has been:
String dateTimePattern = "dd/MM/yyyy HH:mm:ss";
SimpleDateFormat dateTimeFormat = new SimpleDateFormat(dateTimePattern);
Date feUltModDateTime = dateTimeFormat.parse(feUltMod);
feUltMod = feUltModDateTime.toString();
But I'm getting a parse error:
java.text.ParseException: Unparseable date: "2018-07-20 18:53:46.598000 +02:00:00"
java.time
DateTimeFormatter origFormatter
= DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSS XXXXX");
DateTimeFormatter desiredFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu HH:mm:ss");
ZoneId desiredZone = ZoneId.of("America/Fort_Nelson");
String feUltMod = "2018-07-20 18:53:46.598000 +02:00:00";
OffsetDateTime dateTime = OffsetDateTime.parse(feUltMod, origFormatter);
ZonedDateTime dateTimeWithTimeZoneApplied = dateTime.atZoneSameInstant(desiredZone);
feUltMod = dateTimeWithTimeZoneApplied.format(desiredFormatter);
System.out.println(feUltMod);
Output from this snippet is:
20/07/2018 09:53:46
Generally you need two formatters for converting a date or date-time from one format to another: one that specifies the format to convert from and one that specifies the format to convert to.
into a DateTime object like this
A date-time object doesn’t have a format, so in that respect cannot be “like this”. dateTimeWithTimeZoneApplied in the above snippet is in the specified time zone, so has the hours adjusted. After converting to this time zone I have formatted into a string in the format you mentioned, in case you wanted this (I didn’t find it clear).
I am using and recommending java.time, the modern Java date and time API. The date and time classes you were using, Date and SimpleDateFormat, are long outdated and poorly designed, it’s not worth struggling with them. Also SimpleDateFormat supports only milliseconds so can only work correctly with exactly 3 decimals on the seconds, not with the 6 decimals you have got.
Link: Oracle tutorial: Date Time explaining how to use java.time.
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date df = new Date();
String yourString = sdf.format(df);
Date parsedDate = sdf.parse(yourString);
Timestamp sqlDate = new java.sql.Timestamp(parsedDate.getTime());
The above code will give you current Timestamp.Timestamp will provide better feasibilty

Date format changes using Joda API

I m using joda API in my code but i am unable to show the dates in correct format:
Sample COde:
DateTimeZone gmtTimeZone = DateTimeZone.forID("GMT");
DateTimeZone pstTimeZone = DateTimeZone.forID("America/Chicago");
DateTimeFormatter startEndFormat = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").withZone(pstTimeZone);
DateTimeFormatter startEndOutFormat = DateTimeFormat.forPattern("dd-MM-yyyy HH:mm:ss").withZone(gmtTimeZone);
DateTimeFormatter batchFormat = DateTimeFormat.forPattern("dd-MMM-yyyy");
DateTimeFormatter durationFormat = DateTimeFormat.forPattern("hh:mm:ss");
on running the code the time value is changed.
for Example:
"30-09-2015 16:21:48"
Which should be:
"30-09-2015 04:21:48"
You have to learn java data formatting characters. see following table which i found at SimpleDateFormat Documentation
So,
change your dd-MM-yyyy HH:mm:ssdate format into dd-MM-yyyy hh:mm:ss

Java SimpleDateFormat parse Timezone like America/Los_Angeles

I want to parse the following string in Java and convert it to a date:
DTSTART;TZID=America/Los_Angeles:20140423T120000
I tried this:
SimpleDateFormat sdf = new SimpleDateFormat("'DTSTART;TZID='Z':'yyyyMMdd'T'hhmmss");
Date start = sdf.parse("DTSTART;TZID=America/Los_Angeles:20140423T120000");
And this:
SimpleDateFormat sdf = new SimpleDateFormat("'DTSTART;TZID='z':'yyyyMMdd'T'hhmmss");
Date start = sdf.parse("DTSTART;TZID=America/Los_Angeles:20140423T120000");
But it still doesn't work. I think the problem is in America/Los_Angeles.
Can you help me please?
Thank you
Try this one using TimeZone.
Note: You have to split your date string before doing this operation.
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'hhmmss");
TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
sdf.setTimeZone(tz);
Date start = sdf.parse("20140423T120000");
In SimpleDateFormat pattern Z represent RFC 822 4-digit time zone
For more info have a look at SimpleDateFormat#timezone.
If you look for a solution how to parse the whole given string in one and only one step then Java 8 offers this option (the pattern symbol V is not supported in SimpleDateFormat):
// V = timezone-id, HH instead of hh for 24-hour-clock, u for proleptic ISO-year
DateTimeFormatter dtf =
DateTimeFormatter.ofPattern("'DTSTART;TZID='VV:uuuuMMdd'T'HHmmss");
ZonedDateTime zdt =
ZonedDateTime.parse("DTSTART;TZID=America/Los_Angeles:20140423T120000", dtf);
Instant instant = zdt.toInstant();
// if you really need the old class java.util.Date
Date jdkDate = Date.from(instant);

Joda Date Format Issue

I am trying to use the following code, but I am getting an error Invalid format: "12/11/2013":
String dFrom = ps.utils.gv(request, "dFrom");
String dTo = ps.utils.gv(request, "dTo");
DateTime dateFrom = new DateTime(dFrom);
DateTime dateTo = new DateTime(dTo);
int weeks = Weeks.weeksBetween(dateFrom, dateTo).getWeeks();
Could somebody please provide an example of how to format the date variable dFrom which is typically a UK formatted date such as 12/11/2013 to an ISO Date such as 2013-11-12 which I believe Joda supports.
Any help would be much appreciated :-)
If you want convert format 12/11/2013 to 2013-11-12, you can use
DateTimeFormatter dtf = DateTimeFormat.forPatter("dd/MM/yyyy"); // or MM/dd/yyyy ?
String isoDate = ISODateTimeFormat.date().print(dtf.parseDateTime("12/11/2013"));
For ISO format 2013-11-12 you can use standart date formatter:
ISODateTimeFormat::date()
DateTime date = ISODateTimeFormat.date().parseDateTime("2013-11-12");
String dateAsString = ISODateTimeFormat.date().print(date);
For format 12/11/2013 you should create your own formatter
DateTimeFormatter dtf = DateTimeFormat.forPatter("dd/MM/yyyy"); // or MM/dd/yyyy ?
DateTime date = dtf.parseDateTime("12/11/2013");
String dateAsString = dtf.print(date);

Java datetime format convert

date here my problem:
String datetime = "2012-03-24 23:20:51";
I know that that string is in UTC timezone.
I need to convert this string to format "yyy-mm-dd'T'HH:mm:ssZ".
To do this I'm using following code:
SimpleDateFormat inFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
inFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date inDate = inFormatter.parse(datetime);
SimpleDateFormat outFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
outFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
String output = outFormatter.format(inDate);
The problem is that this code is running on a server with timezone UTC+1 and the result it gave me is this:
output = "2012-03-24T21:20:51+0000"
It removes 2 hours from the initial time and puts the UTC timestamp (0000).
Can you please help me solving this?
Thank you.
If the output format is UTC+1 then you should use that in the outformatter instead of UTC.
outFormatter.setTimeZone(TimeZone.getTimeZone("UTC+01:00"));
Also, if you don't want the +0000 at the end then remove the Z
SimpleDateFormat outFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

Categories