Unable to obtain LocalDateTime from TemporalAccessor when parsing LocalDateTime (Java 8)

I am simply trying to convert a date string into a DateTime object in Java 8. Upon running the following lines: DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“yyyyMMdd”); LocalDateTime dt = LocalDateTime.parse(“20140218”, formatter); I get the following error: Exception in thread “main” java.time.format.DateTimeParseException: Text ‘20140218’ could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to … Read more

How do I get the AM/PM value from a DateTime?

The code in question is below: public static string ChangePersianDate(DateTime dateTime) { System.Globalization.GregorianCalendar PC = new System.Globalization.GregorianCalendar(); PC.CalendarType = System.Globalization.GregorianCalendarTypes.USEnglish; return PC.GetYear(dateTime).ToString() + “https://stackoverflow.com/” + PC.GetMonth(dateTime).ToString() + “https://stackoverflow.com/” + PC.GetDayOfMonth(dateTime).ToString() + “” + PC.GetHour(dateTime).ToString() + “:” + PC.GetMinute(dateTime).ToString() + “:” + PC.GetSecond(dateTime).ToString() + ” ” ???????????????? } how can I get the AM/PM from the … Read more

UnsupportedTemporalTypeException when formatting Instant to String

I’m trying to format an Instant to a String using the new Java 8 Date and Time API and the following pattern: Instant instant = …; String out = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss”).format(instant); Using the code above I get an exception which complains about an unsupported field: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: YearOfEra at java.time.Instant.getLong(Instant.java:608) at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298) … 7 … Read more

Python datetime to string without microsecond component

I’m adding UTC time strings to Bitbucket API responses that currently only contain Amsterdam (!) time strings. For consistency with the UTC time strings returned elsewhere, the desired format is 2011-11-03 11:07:04 (followed by +00:00, but that’s not germane). What’s the best way to create such a string (without a microsecond component) from a datetime … Read more

Format JavaScript date as yyyy-mm-dd

I have a date with the format Sun May 11,2014. How can I convert it to 2014-05-11 using JavaScript? function taskDate(dateMilli) { var d = (new Date(dateMilli) + ”).split(‘ ‘); d[2] = d[2] + ‘,’; return [d[0], d[1], d[2], d[3]].join(‘ ‘); } var datemilli = Date.parse(‘Sun May 11,2014’); console.log(taskDate(datemilli)); The code above gives me the … Read more

Given a DateTime object, how do I get an ISO 8601 date in string format?

Given: DateTime.UtcNow How do I get a string which represents the same value in an ISO 8601-compliant format? Note that ISO 8601 defines a number of similar formats. The specific format I am looking for is: yyyy-MM-ddTHH:mm:ssZ 18 s 18 Note to readers: Several commenters have pointed out some problems in this answer (related particularly … Read more

Where can I find documentation on formatting a date in JavaScript?

I noticed that JavaScript’s new Date() function is very smart in accepting dates in several formats. Xmas95 = new Date(“25 Dec, 1995 23:15:00”) Xmas95 = new Date(“2009 06 12,12:52:39”) Xmas95 = new Date(“20 09 2006,12:52:39”) I could not find documentation anywhere showing all the valid string formats while calling new Date() function. This is for … Read more