I am trying to get the current timestamp and convert it into a UTC date for an XML file.
I am using this
import java.util.Date;
import java.util.TimeZone;
import com.google.gwt.i18n.client.DateTimeFormat;
DateTimeFormat.format( new Date(), TimeZone.getTimeZone("UTC"));
but am getting the following error
The method format(Date, TimeZone) in
the type DateTimeFormat is not
applicable for the arguments (Date,
TimeZone)
I need the output as "yyyy-mm-ddThh:mm:ssZ"
Use com.google.gwt.i18n.client.TimeZone.createTimeZone(0) to create a UTC TimeZone object, and then use that in DateTimeFormat.format(Date, TimeZone).
You can use apostrophe to indicate literals in a DateTimeFormat pattern.
eg. "HH'o''clock'"
So, the formatter you need would look something like this:
DateTimeFormat formatter = DateTimeFormat.getFormat("yyyy-mm-dd'T'HH:mm:ssZ");
I tried it out. It gave me an output in format 2010-16-29T08:16:23+0530
Is this what you are looking for?
You should provide a com.google.gwt.i18n.client.TimeZone instead of java.util.TimeZone
Related
Is there a way to change the date format in AnyLogic, without constructing something with the implemented Time functions?
I want my Date to be displayed in such a way
"E dd.MM.yyyy HH:mm"
With the timefunctions I have problems, to get the date into the right format
Not sure what that E stands for, but if it's the timezone, you can get it with this:
ZoneId.of("Europe/Oslo").getDisplayName(TextStyle.SHORT_STANDALONE, Locale.ENGLISH)
I used Europe/Oslo there, but you can find the zone ids that you need here:
https://docs.oracle.com/middleware/12211/wcs/tag-ref/MISC/TimeZones.html
to use this you need to import libraries in Main/Advanced Java/Imports section
import java.time.ZoneId;
import java.time.format.TextStyle;
And then to get the required code with that format:
"E "+String.format("%02d", getDayOfMonth())+"."
+String.format("%02d",getMonth()+1)+"."
+String.format("%04d",getYear())+" "
+String.format("%02d",getHourOfDay())+":"
+String.format("%02d",getMinute())
You can replace the "E " with what I explained before
Or you can use whatever is currently your timezone... AnyLogic uses your computer timezone in order to know what timezone you are in:
Calendar cal=Calendar.getInstance();
cal.setTime(date());
String timezoneCode=cal.getTimeZone().toZoneId().getDisplayName(TextStyle.SHORT_STANDALONE, Locale.ENGLISH);
The question is kind of similar with the problem: Change the timestamp to UTC format in Pyspark
Basically, it is convert timestamp string format ISO8601 with offset to UTC timestamp string(2017-08-01T14:30:00+05:30 -> 2017-08-01T09:00:00+00:00 ) using scala.
I am kind of new to scala/java, I checked spark library which they dont have a way to convert without knowing the timezone, which I dont have a idea of timezone unless (I parse it in ugly way or using java/scala lib?) Can someone help?
UPDATE: The better way to do this: setup timezone session in spark, and use df.cast(DataTypes.TimestampType) to do the timezone shift
org.apache.spark.sql.functions.to_utc_timestamp:
def to_utc_timestamp(ts: Column, tz: String): Column
Given a timestamp like '2017-07-14 02:40:00.0', interprets it as a time in the given time zone, and renders that time as a timestamp in UTC. For example, 'GMT+1' would yield '2017-07-14 01:40:00.0'.
You can use the java.time primitives to parse and convert your timestamp.
scala> import java.time.{OffsetDateTime, ZoneOffset}
import java.time.{OffsetDateTime, ZoneOffset}
scala> val datetime = "2017-08-01T14:30:00+05:30"
datetime: String = 2017-08-01T14:30:00+05:30
scala> OffsetDateTime.parse(datetime).withOffsetSameInstant(ZoneOffset.UTC)
res44: java.time.OffsetDateTime = 2017-08-01T09:00Z
When I print my DateTime with myDate.toIso8601String();
it prints
2015-11-15T11:55:32.250
Which doesn't validate with an online validator I tried. The spec says it should be of one of the following formats:
2002-10-02T10:00:00-05:00
2002-10-02T15:00:00Z
2002-10-02T15:00:00.05Z
I could try adding a Z at the end, but that seems hacky. Is there a way to print a DateTime in Dart so it complies to one of the following formats?
Z is for UTC. You get it if you convert your DateTime to an UTC time with
new DateTime().now().toUtc().toIso8601String();
Try it in DartPad
I'm using joda time to format my ISO Date input string, but I'm getting an exception that my ISO Date is malformed:
Invalid format: "2014-06-20T11:41:08+02:00" is malformed at "+02:00"
This is my code:
val formatter: DateTimeFormatter = ISODateTimeFormat.dateTime.withZone(DateTimeZone.getDefault)
val date: DateTime = formatter.parseDateTime("2014-06-20T11:41:08+02:00")
What's wrong here?
The error comment is slightly misleading here, as Joda formatter you derive from ISODateTimeFormat expects the millisecond part of the date/time string to be present, therefore the following will work fine:
val formatter: DateTimeFormatter = ISODateTimeFormat.dateTime().withZone(DateTimeZone.getDefault())
val date: DateTime = formatter.parseDateTime("2014-06-20T11:41:08.0+02:00")
The answer by Radyk is correct.
ISO 8601 Formats Built-In
However, you needn't specify a formatter at all. The DateTime class has a built-in parser for your ISO 8601 compliant format, used automatically by the constructor.
DateTime dateTime = new DateTime( "2014-06-20T11:41:08+02:00", timeZone );
While the second argument is optional, I suggest you assign a DateTimeZone object to be assigned to the DateTime if you know such a time zone. The input string has an offset-from-UTC, but a time zone is more than just an offset. A time zone includes rules for Daylight Saving Time and other anomalies. Use proper time zone names, never 3 or 4 letter codes like EST or IST.
Other Formats
You can apply many other formats:
Built-in ISO 8601 formatters
Built-in localized (short, medium, long, and full formats, Locale-sensitive)
Custom specified by you.
For example, if you want only the date portion without the time-of-day in your String representation, call ISODateTimeFormat.date() to access a built-in formatter.
Example code in Joda-Time 2.8.
String output = ISODateTimeFormat.date().print( dateTime ); // Format: yyyy-MM-dd
Search StackOverflow for hundreds of other Questions and Answers about formatting date-time values.
I have a string 7/24/2013 6:05:00 PM and want to convert it to a DateTime object.
I am using
DateTime newDate = DateTime.ParseExact(date,"M/d/yyyy h:mm:ss tt",
System.Globalization.CultureInfo.InvariantCulture);
but the newDate object is being 09.07.2013 06:45:00. I want it as it is seen above the string version.
Do you have any idea why it is not converted the format I wanted or any opinion would be great how I can render it as a datetime object.
Thank you
As your date is already in a common format style you should try parsing using the current UI culture, in your case en-US.
E.g.
DateTime.ParseExact(date,"M/d/yyyy h:mm:ss tt", System.Globalization.CultureInfo.CurrentCulture);
By using the InvariantCulture the parsing is ignoring all cultural clues whilst parsing your string.
For more information: http://msdn.microsoft.com/en-us/library/dd465121.aspx