Questions tagged [datetime-parsing]
423 questions
19
votes
6 answers
Is there a way to find the first string that matches a DateTime format string?
Given a date time format string, is there a standard way to find the first matching substring that matches that format?
for example, given...
d-MMM-yy H:mm:ss
and some text...
"blah 1 2 3 7-Jul-13 6:15:00 4 5 6 blah"
I'd expect it to return…

Keith Nicholas
- 43,549
- 15
- 93
- 156
18
votes
3 answers
How to check if string matches date pattern using time API?
My program is parsing an input string to a LocalDate object. For most of the time the string looks like 30.03.2014, but occasionally it looks like 3/30/2014. Depending on which, I need to use a different pattern to call…
user1019830
14
votes
2 answers
DateTimeFormatter weekday seems off by one
I'm porting an existing application from Joda-Time to Java 8 java.time.
I ran into a problem where parsing a date/time string that contains a 'day of week' value triggered an exception in my unit tests.
When parsing:
2016-12-21 20:50:25 Wednesday…

Niels Basjes
- 10,424
- 9
- 50
- 66
13
votes
1 answer
Why doesn't DateTime.ParseExact parse UTC format with the trailing Z?
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"…

scope_creep
- 4,213
- 11
- 35
- 64
13
votes
1 answer
Java 8 DateTimeFormatter to ignore millisecond and zone
I am struggling with Java 8 DateTimeFormatter.
I would like to convert a given String to dateFormat and parse to LocalDateTime
Here is my code
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")
String text =…

J. Doem
- 619
- 7
- 21
13
votes
1 answer
Why does this date parsing fail?
I'm trying to convert a string into a LocalDateTime object.
@Test
public void testDateFormat() {
String date = "20171205014657111";
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
LocalDateTime dt =…

user4184113
- 976
- 2
- 11
- 29
12
votes
7 answers
Create DateTime from string without applying timezone or daylight savings
How do I create a DateTime var from a string which is already adjusted for UTC? I am running this on a machine set to BST (GMT+1). If I run the following line of code:
DateTime clientsideProfileSyncStamp = Convert.ToDateTime("20-May-2011…

Journeyman
- 10,011
- 16
- 81
- 129
12
votes
1 answer
Jackson: parse custom offset date time
I have a model which has a timestamp property:
class Model {
@JsonProperty("timestamp")
private OffsetDateTime timestamp;
}
The format of the timestamp is as following:
2017-09-17 13:45:42.710576+02
OffsetDateTime is unable to parse…

Kwers T
- 123
- 1
- 1
- 4
12
votes
2 answers
How to create DateTimeformatter with optional seconds arguments
I am trying to create a DateTimeformatter to validate following date times:
String date1 = "2017-07-06T17:25:28";
String date2 = "2017-07-06T17:25:28.1";
String date3 = "2017-07-06T17:25:28.12";
String date4 = "2017-07-06T17:25:28.123";
String date5…

Amit Garg
- 838
- 1
- 10
- 21
10
votes
6 answers
Java 8 Time API - ZonedDateTime - specify default ZoneId when parsing
I am trying to write a generic method to return a ZonedDateTime given a date as String and its format.
How do we make ZonedDateTime to use the default ZoneId if it is not specified in the date String?
It can be done with java.util.Calendar, but I…

RuntimeException
- 1,593
- 2
- 22
- 31
10
votes
4 answers
How to convert a given time (String) to a LocalTime?
I will be asking a user to enter a specific time: 10AM, 12:30PM, 2:47PM, 1:09AM, 5PM, etc.
I will be using a Scanner to get the user's input.
How can I parse/convert that String to a LocalTime object? Is there any built-in function in Java that…

SVCS1994
- 213
- 1
- 3
- 14
10
votes
2 answers
TryParseExact returns false, though I don't know why
Method TryParseExact in code block below returns true.
I would like to know why.
I think this date "2013.03.12" is invalid because this is not separated by slash but dot.
After I changed the CultureInfo "de-De" to "en-US", the method returns false.…

Nigiri
- 3,469
- 6
- 29
- 52
9
votes
1 answer
Customize a Locale in Java
In Java the Locale defines things that are related how people want to see things (like currency formats, the name of the months and when a week starts).
When parsing the name of a Month (with a DateTimeFormatter) it starts to become tricky.
If you…

Niels Basjes
- 10,424
- 9
- 50
- 66
9
votes
1 answer
How to parse the expiration date of a JWT to a time.Time() in Go?
I'd like to parse the expiration date (exp) from a JSON Web Token (JWT) without verifying it. I've tried the following script (in an attempt to follow How to parse unix timestamp to time.Time):
package main
import (
"fmt"
"log"
…

Kurt Peek
- 52,165
- 91
- 301
- 526
9
votes
6 answers
Failed to parse single digit hour and lowercase am-pm of day into Java 8 LocalTime
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…

benstpierre
- 32,833
- 51
- 177
- 288