datetime-parsing

Java 8 DateTimeFormatter parsing for optional fractional seconds of varying significance

狂风中的少年 提交于 2019-11-30 17:15:20
My MCVE (as a TestNG unit test): public class MyDateTimeFormatterTest { private static final String BASE_PATTERN = "yyyy/MM/dd HH:mm:ss"; private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern(BASE_PATTERN + "[.SSSSSSSSS]"); private static final LocalDateTime TEST_INPUT = LocalDateTime.of(2015, 5, 4, 12, 34, 56, 123456789); @DataProvider(name = "test-cases") public Iterator<Object[]> getTestCases() { return Arrays.asList(testFor("", ChronoUnit.SECONDS), testFor(".SSS", ChronoUnit.MILLIS), testFor(".SSSSSS", ChronoUnit.MICROS), testFor(".SSSSSSSSS", ChronoUnit.NANOS))

parsing date string in python (convert string to date)

我怕爱的太早我们不能终老 提交于 2019-11-30 15:35:42
问题 I have a datetime string in the form of a string as: 2011-10-23T08:00:00-07:00 How do i parse this string as the datetime object. I did the following reading the documentation: date = datetime.strptime(data[4],"%Y-%m-%d%Z") BUt I get the error ValueError: time data '2011-10-23T08:00:00-07:00' does not match format '%Y-%m-%d%Z' which is very clear. But I am not sure how to read this format. Any suggestions. Thanks Edit: Also, I must add, all I care about is the date part 回答1: Standard datetime

parsing date string in python (convert string to date)

跟風遠走 提交于 2019-11-30 14:23:47
I have a datetime string in the form of a string as: 2011-10-23T08:00:00-07:00 How do i parse this string as the datetime object. I did the following reading the documentation: date = datetime.strptime(data[4],"%Y-%m-%d%Z") BUt I get the error ValueError: time data '2011-10-23T08:00:00-07:00' does not match format '%Y-%m-%d%Z' which is very clear. But I am not sure how to read this format. Any suggestions. Thanks Edit: Also, I must add, all I care about is the date part eumiro Standard datetime.datetime.strptime has problems with timezone definitions. Use dateutil.parser >>> from dateutil

Why doesn't DateTime.ParseExact parse UTC format with the trailing Z?

眉间皱痕 提交于 2019-11-30 14:03:14
问题 Another ParseExact problem. I'm trying to parse a UTC formatted string to a datetime with the format of: "YYYY-MM-DDThh:mm:ss.ssZ" which is in UTC format, with the trailing Z. I can't parse exact it for some reason. I have tried the "u", "s", "o" custom format strings, as well as as several DateTimeStyles and well as handwritten. The culture is invariant. For some reason it doesn't like the Z, which indicates it's a UTC string. When I remove it, parses. I would happily, with some satisfaction

Failed to parse single digit hour and lowercase am-pm of day into Java 8 LocalTime

青春壹個敷衍的年華 提交于 2019-11-30 05:39:57
问题 When I try to run the following code: LocalTime test = LocalTime.parse("8:00am", DateTimeFormatter.ofPattern("hh:mma")); I get this: Exception in thread "main" java.time.format.DateTimeParseException: Text '8:00am' could not be parsed at index 0 Any idea why this might be happening? 回答1: The AM/PM tokens have* to be uppercase: LocalTime test = LocalTime.parse("8:00AM", DateTimeFormatter.ofPattern("hh:mma")); Patterns definitions for hours: Symbol Meaning Presentation Examples ------ ---------

ParseException when parsing 3 character abbreviated month using SimpleDateFormat

坚强是说给别人听的谎言 提交于 2019-11-29 18:52:34
Here is my code, SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); Date dft = (Date) format.parse("16-MAY-2018 09:30:22:000"); I am getting below exception java.text.ParseException: Unparseable date: "16-MAY-2018 09:30:22" What's to be used to parse milliseconds? The pattern should be MMM because there are three characters in the month. You should also prefer java.time classes to the ones you're currently using if you're on Java 8 or above: DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendPattern("dd-MMM-yyyy") .appendLiteral(' ') .append(ISO_LOCAL_TIME)

How to unify date format using DateTimeFormatter

前提是你 提交于 2019-11-29 12:14:57
I need to parse different time format into BASIC_ISO_DATE . Right now, there are 4 types of date format: 2016-10-01 (ISO_LOCAL_DATE) 2016T 201610T 2016-02-07T22:03:39.937Z (ISO 8601) Need to parse to 20161001 and print out, with default day is 01 , default month Jan . Examples: 2016T -> 20160101 201610T -> 20161001 How can I use DateTimeFormatter to achieve this? Just to complement @Flown's answer (which works perfectly BTW), you can also use optional patterns (delimited by [] ): DateTimeFormatter parser = new DateTimeFormatterBuilder() // optional ISO8601 date/time and offset .appendOptional

Parse 'Date & Time' string in Javascript which are of custom format

孤人 提交于 2019-11-29 01:35:53
I have to parse a date and time string of format "2015-01-16 22:15:00". I want to parse this into JavaScript Date Object. Any help on this? I tried some jquery plugins, moment.js, date.js, xdate.js. Still no luck. With moment.js you can create a moment object using the String+Format constructor : var momentDate = moment('2015-01-16 22:15:00', 'YYYY-MM-DD HH:mm:ss'); Then, you can convert it to JavaScript Date Object using toDate() method : var jsDate = momentDate.toDate(); new Date("2015-01-16T22:15:00") See Date.parse() . The string must be in the ISO-8601 format. If you want to parse other

Parse Date String in Java [duplicate]

别等时光非礼了梦想. 提交于 2019-11-28 14:23:05
This question already has an answer here: Converting ISO 8601-compliant String to java.util.Date 26 answers Java - SimpleDateFormat formatter to return epoch time with milliseconds [duplicate] 6 answers I have date in the format "yyyy-MM-dd'T'HH:mm:ss.sssZ". for example the date is "2018-07-17T09:59:51.312Z". I am using the below code to parse the String in Java. String date="2018-07-17T09:59:51.312Z"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sssZ"); Date transactionDateTime = simpleDateFormat.parse(date); This is giving me "Unparseable date:" Exception.

Cannot parse the date in Python

回眸只為那壹抹淺笑 提交于 2019-11-28 14:20:27
I need to parse date and time. Here is what I've got: import time a = time.strptime('Apr 28 2013 23:01', "%b %d %y %H:%M") print a But it gives me Traceback (most recent call last): File "/home/aaa/Documents/python_test.py", line 17, in <module> a = time.strptime('Apr 28 2013 23:01', "%b %d %y %H:%M") File "/usr/lib/python2.7/_strptime.py", line 467, in _strptime_time return _strptime(data_string, format)[0] File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime (data_string, format)) ValueError: time data 'Apr 28 2013 23:01' does not match format '%b %d %y %H:%M' What am I doing wrong