I need to change the SimpleDateFormat to some other format which is equivalent in jodatime.
Here's the code which needs to be changed.
public static String dateFormat(Date date, String format)
{
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
I have tried to use DateTimeFormatter.
public static String dateFormat(DateTime date, String format)
{
DateTimeFormatter dtf = DateTimeFormat.forPattern(format);
DateTime tempDateTime = dtf.parseDateTime(date.toString());
return tempDateTime.toString();
}
But I am getting Error.
Well, I look for you in the documentation of Joda Time and SimpleDateFormat. As you can see there, the pattern definitions are unfortunately not the same. If you translate from SimpleDateFormat to Joda-DateTimeFormat, then you have to note following items:
Change Y to x (so called week-year).
Change y to Y (year-of-era) and maybe changing the chronology, too (from ISO to Gregorian/Julian)
W is not supported in Joda Time (week of month), hence no replacement!
F is not supported in Joda Time (day of week in month), hence no replacement!
Change u to e (day number of week - ISO order, not localized), available since Java 7.
The symbol S is handled differently (I suppose in Joda Time better because of correct zero padding).
The zone symbol z is in Joda Time not allowed for parsing (maybe this is the current cause of your problems - you have not shown your pattern format or your exception yet).
The zone/offset symbol Z is handled better in Joda Time, for example allows colons in offset etc. If you need latter you can use X in SimpleDateFormat which has the replacement Z in Joda Time.
Some tests added:
Following sample code demonstrates the different handling of format symbol S.
String s = "2014-01-15T14:23:50.026";
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSS");
DateTime instant = dtf.parseDateTime(s);
System.out.println(dtf.print(instant)); // 2014-01-15T14:23:50.0260
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSS");
Date date = sdf.parse(s);
System.out.println(sdf.format(date)); // 2014-01-15T14:23:50.0026 (bad!)
Another test for format symbol z (is that your problem???):
String s = "2014-01-15T14:23:50.026PST";
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz");
DateTime instant = dtf.parseDateTime(s);
System.out.println(dtf.print(instant)); // abort
Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "2014-01-15T14:23:50.026PST" is malformed at "PST"
at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:866)
at time.JodaTest8.main(JodaTest8.java:83)
SimpleDateFormat can do this zone name parsing (although at least dangerous sometimes):
String s = "2014-01-15T14:23:50.026PST";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz");
Date date = sdf.parse(s);
System.out.println(sdf.format(date)); // 2014-01-15T14:23:50.026PST
Related
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.
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
I have tried to get the date format 2016-08-29T09:15:17Z but not able to get the trailing Z at the end.
I also checked the date time documentation at official website but could not find a similar pattern. So far the date format I have created is as follows:
yyyy-MM-dd'T'HH:mm:ss.SSSZ
So far the code I have written is:
public static void main(String args[]) throws ParseException{
Date nDate=new Date();
//SimpleDateFormat format=new SimpleDateFormat("ddMMYYYYHHMMSS");
String date=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(nDate);
System.out.println(date);
}
The T is just a literal to separate the date from the time, and the Z means "zero hour offset" also known as "Zulu time" (UTC). If your strings always have a "Z" you can use -
TimeZone timeZone = TimeZone.getTimeZone("UTC"); // optional
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); // Quoted "Z" to indicate UTC, no timezone offset
dateFormat.setTimeZone(timeZone); // optional
String date = dateFormat .format(new Date()); // date will have the required format
System.out.println(date);
Z is a constant value like T in your string, so you have to put single quotes arround it:
String date=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").format(nDate);
If you use Z without single quotes DateFormat expect the timezone value
Oracle Documentation describe that z and Z both are use for time zone.
So if you don't want special meaning of Z then you need to write like :
yyyy-MM-dd'T'HH:mm:ss'Z'
I am adding another way to do it, using the new java.time API introduced from JDK 8 onwards.
LocalDateTime nDate=LocalDateTime.now();
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral('T')
.append(DateTimeFormatter.ISO_LOCAL_TIME)
.appendLiteral('Z')
.toFormatter();
String date = formatter.format(nDate);
System.out.println(date);
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");
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);