After a week of going through so many examples, and moving from Java Date,
to Calendar, to Joda. I have decided to seek help from other sources.
The problem:
Our table has two fields Date (Timestamp), and TZ (String). The idea is to store
the user's UTC in timestamp, and timezone, well, you get the idea. So basically
we think in UTC, and present the user with the time converted to their
timezone on the front end (ie, using the value store in table.TZ)
Another requirement is to use the proper Object (Date, DateTime whatever).
And not pass a String representation of the date around. The best would
be a valid Long that will be correctly translated by MySQL, without having
to use the FROM_UNIXTIME mysql function in our query.
Code we are using:
public DateTime convertTimezone(LocalDateTime date, DateTimeZone srcTZ, DateTimeZone dstTZ, Locale l) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").withLocale(l);
DateTime srcDateTime = date.toDateTime(srcTZ);
DateTime dstDateTime = srcDateTime.toDateTime(dstTZ);
System.out.println(formatter.print(dstDateTime));
System.out.println(formatter.parseDateTime(dstDateTime.toString()));
return formatter.parseDateTime(formatter.print(dstDateTime));
}
The String output is exactly what we need (ie UTC time, 2013-08-23 18:19:12),
but the formatter.parseDateTime(dstDateTime.toString() is crashing with the following
error. Probably because of the UTC timezone independent info, and milleseconds?:
Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "2013-08- 23T18:19:12.515Z" is malformed at "T18:19:12.515Z"
at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:873)
at com.example.business.rate.RateDeck.convertTimezone(RateDeck.java:75)
at com.example.business.rate.RateDeck.WriteData(RateDeck.java:143)
at com.example.business.rate.RateDeck.main(RateDeck.java:64)
Search engine enriched question:
How to format UTC for Joda DateTime.
PS My first SO post, and it feels nice? :)
Thanks in Advance,
The new fixed version:
public Timestamp convertTimezone(LocalDateTime date, DateTimeZone srcTZ, DateTimeZone dstTZ, Locale l) {
DateTime srcDateTime = date.toDateTime(srcTZ);
DateTime dstDateTime = srcDateTime.toDateTime(dstTZ);
return new Timestamp(dstDateTime.getMillis());
}
Nick.
It's simply crashing because the format of the parsed string doesn't match with the format of the formatter.
The formatter parses using the format yyyy-MM-dd HH:mm:ss, and the toString() method of DateTime formats the date it using (as documented) the ISO8601 format (yyyy-MM-ddTHH:mm:ss.SSSZZ).
Related
I'm learning Java and come across this issue. I have a date string with the given format.
String dbTime = "01/01/1998 12:30:00";
final String DATE_FORMAT = "MM/dd/yyyy HH:mm:ss";
Now I wanted to initialize/create a Date object of UTC timezone.
For this, I have tried below code
SimpleDateFormat sdfAmerica = new SimpleDateFormat(DATE_FORMAT);
TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
sdfAmerica.setTimeZone(utcTimeZone);
String sDateInAmerica = sdfAmerica.format(date); // Convert to String first
Date dateInAmerica = new Date();
try {
dateInAmerica = formatter.parse(sDateInAmerica); // Create a new Date object
} catch (ParseException e) {
e.printStackTrace();
}
This will convert the time into UTC instead of just creating a date object.
01/02/1998 23:00:00
Now I'm confused as to which is the correct approach to convert the time.
I have time in string format and I have to convert it into different formats mainly UTC to PST or PST to UTC.
After some research, I found this tutorial but was unable to get the expected output.
The java.util.Date class is not optimal to start with. While it looks like a full date from the outside, it actually only represents a timestamp without storing actual timezone information.
On Java 8 and later I'd suggest to stick with the better designed java.time.* classes.
String dbTime = "01/01/1998 12:30:00";
String DATE_FORMAT = "MM/dd/yyyy HH:mm:ss";
// parsed date time without timezone information
LocalDateTime localDateTime = LocalDateTime.parse(dbTime, DateTimeFormatter.ofPattern(DATE_FORMAT));
// local date time at your system's default time zone
ZonedDateTime systemZoneDateTime = localDateTime.atZone(ZoneId.systemDefault());
// value converted to other timezone while keeping the point in time
ZonedDateTime utcDateTime = systemZoneDateTime.withZoneSameInstant(ZoneId.of("UTC"));
// timestamp of the original value represented in UTC
Instant utcTimestamp = systemZoneDateTime.toInstant();
System.out.println(utcDateTime);
System.out.println(utcTimestamp);
As you can see from the names alone there are different classes for different use-cases of dates.
java.time.LocalDateTime for example only represents a date and time without a specific timezone context and therefore can be used to parse your string value directly.
To convert timezones, you first have to convert into the a ZonedDateTime, which accepts date, time and timezone. I've intialized the sample on "systemDefault", as on most smaller apps you can use the JVM and OS'es default value to assume the current timezone.
You could also use ZoneId.of("America/Los_Angeles") directly if you want to make sure the value is interpreted as pacific time.
This value can be converted into another ZonedDateTime in another timezone, e.g. UTC.
For UTC especially you could also use the Instant class, which represents only a UTC timestamp and can also be used as a basis for most other types
Firstly, I'm a C# dev learning Java. I'm converting a program I wrote in C# as an exercise and am having problems with parsing a date being submitted from an html form. The form is sent as an email and the java program reads the emails and parses the body. I have a drop down calendar for my peeps to select a date from but there's always some jerk who has to type it in and mess everything up. Currently I am doing this in my code:
public void SetDatePlayed(String datePlayed)
{
this.datePlayed = LocalDate.parse(datePlayed);
}
datePlayed being passed in is a string usually formatted as yyyy-MM-dd but of course someone typed in 3/7 instead of using the calendar drop down on the form. this.datePlayed is a LocalDate. In C# I would just end up with a date that assumed 2020 for the year - no problem. LocalDate really wants it in the yyyy-MM-dd format and I don't know what the best practice here is with Java. I've been googling it all morning and haven't come across this as being an issue for anyone else. I don't care if I'm using LocalDate but I do need it to be a date datatype so I can do date checks, sorts, searches, etc later on.
You can use DateTimeFormatterBuilder and parseDefaulting() to supply default value for the year.
Building on the answer by Sweeper, it can be done like this:
static LocalDate parseLoosely(String text) {
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
.appendPattern("[uuuu-M-d][M/d/uuuu][M/d]")
.parseDefaulting(ChronoField.YEAR, Year.now().getValue())
.toFormatter();
return LocalDate.parse(text, fmt);
}
Warning: Do not cache the formatter in e.g. a static field, since it snapshots the year, if the program might be running across New Year's Eve, which a webapp would, unless you add logic to make the cache auto-refresh on year change.
Test
System.out.println(parseLoosely("2019-04-07"));
System.out.println(parseLoosely("2019-4-7"));
System.out.println(parseLoosely("4/7/2019"));
System.out.println(parseLoosely("4/7"));
Output
2019-04-07
2019-04-07
2019-04-07
2020-04-07
I see two possible interpretations of your question. I'm not sure which one it is, so I'll answer both.
How do I parse a date string in a format that has no year, such as M/d (3/7), to a LocalDate?
You don't. A LocalDate by definition must have year, month, and day. If you only have a month and a day, that's a MonthDay:
MonthDay md = MonthDay.parse("3/7", DateTimeFormatter.ofPattern("M/d"));
If you want the current year added to it, you can do it later:
LocalDate ld = md.atYear(Year.now(/*optionally insert time zone*/).getValue());
How do I handle both yyyy-MM-dd and M/d patterns?
Here's one way: create a DateTimeFormatter that recognises both patterns, parse the string to a TemporalAccessor, check if the TemporalAccessor supports the "year" field:
TemporalAccessor ta = DateTimeFormatter.ofPattern("[M/d][yyyy-MM-dd]").parse("3/7");
if (ta.isSupported(ChronoField.YEAR_OF_ERA)) { // yyyy-MM-dd
LocalDate ld = LocalDate.from(ta);
} else if (ta.isSupported(ChronoField.MONTH_OF_YEAR)) { // M/d
MonthDay md = MonthDay.from(ta);
} else {
// user has entered an empty string, handle error...
}
I want to convert date and time to user requested timezone. date and time is in GMT format. i tried got the solution but the final string contains GMT String in resultant date like (2019-09-18T01:44:35GMT-04:00). i don't want GMT String in the resultant output.
public static String cnvtGMTtoUserReqTZ(String date, String format, String timeZone) {
// null check
if (date == null)
return null;
// create SimpleDateFormat object with input format
SimpleDateFormat sdf = new SimpleDateFormat(format);
// set timezone to SimpleDateFormat
sdf.setTimeZone(TimeZone.getTimeZone(timeZone));
try {
// converting date from String type to Date type
Date _date = sdf.parse(date);
// return Date in required format with timezone as String
return sdf.format(_date);
} catch (ParseException e) {
//log.info("Exception in cnvtGMTtoUserReqTime ::: " + e);
}
return null;
}
Actual Output : 2019-09-18T01:44:35GMT-04:00
Expected Output: 2019-09-18T01:44:35-04:00
Proces datetime objects, not strings
Your question is put in the wrong way, which is most likely due to a design flaw in your program. You should not handle date and time as strings in your program. Always keep date and time in proper datetime objects such as Instant, OffsetDateTime and ZonedDateTime. The mentioned classes are from java.time, the modern Java date and time API, which is the best we have for keeping and processing datetime data.
So your question may for example become: How to convert a moment in time to user requested timezone? A moment in time is represented by an Instant object. And the answer to the question is:
ZoneId userRequestedTimeZone = ZoneId.of("America/New_York");
Instant moment = Instant.parse("2019-09-18T05:44:35Z");
ZonedDateTime userDateTime = moment.atZone(userRequestedTimeZone);
System.out.println(userDateTime);
Please substitute your user’s desired time zone where I put America/New_York. Always give time zone in this format (region/city). Output from the snippet as it stands is:
2019-09-18T01:44:35-04:00[America/New_York]
Assuming that you don’t want the [America/New_York] part of the output, format the datetime to the string that you want:
String dateTimeWithNoZoneId = userDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
System.out.println(dateTimeWithNoZoneId);
2019-09-18T01:44:35-04:00
The latter output is in ISO 8601 format. This format is good for serialization, that is, if you need to convert the datetime to a machine readable textual format, for example for persistence or exchange with other systems. While also human readable, it’s not what your user prefers to see. And as I said, it’s certainly not what you should be handling and processing inside your program.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Wikipedia article: ISO 8601
Use these formats:
fromFormat = "yyyy-mm-dd'T'HH:mm:sszXXX"
toFormat = "yyyy-mm-dd'T'HH:mm:ssXXX"
For more details see examples listed here
My app is using JodaTime to manage date parsing and formatting.
I have this timestamp: 2018-07-24T15:30:00-07:00.
How can display it as 3:30pm, regardless of the user's whereabouts?
Following code will print "3:30pm":
DateTimeFormatter iso = ISODateTimeFormat.dateTimeParser().withOffsetParsed();
DateTime tsp = iso.parseDateTime("2018-07-24T15:30:00-07:00");
DateTimeFormatter out = DateTimeFormat.forPattern("h:mma").withLocale(Locale.ENGLISH);
System.out.println(tsp); // 2018-07-24T15:30:00.000-07:00
System.out.println(out.print(tsp).toLowerCase()); // 3:30pm
The main problem is just that the parser does not retain the parsed offset of -7:00 but shifts it to your system timezone unless you also call withOffsetParsed().
My java timestamp has the following format:
YYYY-MM-DD hh:mm:ss.ms
2016-01-08 15:16:44.554
I got it using the following method:
private String getCurrentTimeStamp() {
Date date= new java.util.Date();
return((new Timestamp(date.getTime())).toString());
}
Is there a standardized xml date and Time format for timestamp? The xs: dateTime has the following format: "YYYY-MM-DDThh: mm: SS" And it is not taking into consideration milliseconds.
XML itself does not define any timestamp formats.
XML Schema Part 2: Datatypes Second Edition incorporates ISO 8601 formats by reference. The dateTime format allows but does not require a decimal point followed by arbitrary fractions of a second. For example, 2016-01-08T15:16:44.554
In XML Schema (XSD), all formats of dates and times are well defined.
The java.time framework built into Java 8 and later supports these formats. See Tutorial.
Here are some examples:
// Dates in XML: YYYY-MM-DD
DateTimeFormatter.ISO_LOCAL_DATE.parse("2002-09-24");
// Dates with TimeZone in XML: YYYY-MM-DDZ
DateTimeFormatter.ISO_DATE.parse("2002-09-24Z");
// Dates with TimeZone in XML: YYYY-MM-DD-06:00 or YYYY-MM-DD+06:00
DateTimeFormatter.ISO_DATE.parse("2002-09-24-06:00");
// Times in XML: hh:mm:ss
DateTimeFormatter.ISO_TIME.parse("09:00:00");
DateTimeFormatter.ISO_TIME.parse("09:00:00.5");
// DateTimes in XML: YYYY-MM-DDThh:mm:ss (with an optional TimeZone)
DateTimeFormatter.ISO_DATE_TIME.parse("2002-05-30T09:00:00");
DateTimeFormatter.ISO_DATE_TIME.parse("2002-05-30T09:30:10.5");
DateTimeFormatter.ISO_DATE_TIME.parse("2002-05-30T09:00:00Z");
DateTimeFormatter.ISO_DATE_TIME.parse("2002-05-30T09:30:10.5-06:00");
Durations and Periods however are not perfectly compatible, because they are split in Durations and Periods in Java. Here are however some examples:
Period.parse("P5Y");
Period.parse("P5Y2M10D");
Duration.parse("PT15H");
Duration.parse("-P10D");
If you can't change the schema, you have to change your function
private String getCurrentTimeStamp() {
Date date = new java.util.Date();
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(date);
}
Maybe you mean the standart ISO for Date.
On this thread
Convert Java Date...
See also:
What is Jaxb and why would i use it
There seems to be a format as part of XSD 1.1:
The type xsd:dateTimeStamp represents a specific date
and time in the format CCYY-MM-DDThh:mm:ss.sss.
http://www.datypic.com/sc/xsd11/t-xsd_dateTimeStamp.html