Date format changes using Joda API - java

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

Related

Java, unparsable date error, converting from String to Date

I have this String "2019-10-17T16:00:00+02:00" and I want to convert this String to a Date object, because I want to change the format. I tried this:
SimpleDateFormat sd1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date dt = sd1.parse(mystring);
SimpleDateFormat sd2 = new SimpleDateFormat("yyyy-MM-dd");
String newDate = sd2.format(dt);
System.out.println(newDate);
But I have this message: Unparseable date: "2019-10-17T16:00:00+02:00"
What can I do?
Thank you all
For the sake of being up to date:
Please consider using the modern date and time API java.time.
You can easily parse and format dates, times and date times using it.
See this example:
public static void main(String[] args) {
String d = "2019-10-17T16:00:00+02:00";
OffsetDateTime odt = OffsetDateTime.parse(d, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
System.out.println(odt.format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss Z")));
}
The output is this:
2019/10/17 16:00:00 +0200
That formatter is used be default, so no need to specify. The standard ISO 8601 formats are used by default in java.time.
OffsetDateTime.parse( "2019-10-17T16:00:00+02:00" ) // ISO 8601 formats can be parsed directly, without specifying a `DateTimeFormatter` object.
If you just want to extract the date part of a date time, then you can do that, too by just applying two additional lines of code:
LocalDate date = odt.toLocalDate();
System.out.println(date.format(DateTimeFormatter.ISO_DATE));
The pattern letter for ISO 8601 time zones is X not Z as others have noted.
Use:
SimpleDateFormat sd1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
And parsing will succeed.

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

formatting string into dateformat with time offset

I need to convert a string into a Joda DateTime object, but the code I am using is not doing the conversion correct. My input is 20140722101846-0700, which should convert to something not unlike 2014-07-22T10:18:46-0700. Here is my code, followed by the incorrect output:
String myet = "20140722101846-0700"
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyyMMddhhmmss-hhmm");
DateTime mydt = dtf.parseDateTime(myet);
The resulting (incorrect) output is: 2014-07-22T07:00:46.000-07:00
How can I fix the code above so that is outputs a correct date?
Your DateTimeFormat doesn't have the correct symbols.
M is for month
m is for minutes
Z is used for the time zone offset (-0700)
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyyMMddhhmmssZ");

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);

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

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());

Categories