24.03.2018 02:34:22
how can I get the delimiter ('.'
) from a that kind of date string? Format can be different and delimiter can be these '.','-','/'
also. Is there a way to do it with Java's date format classes?
-
How do you know `'.'` is the delimiter? – lexicore Apr 12 '18 at 14:27
-
2what do you mean "format can be different" – Tim Apr 12 '18 at 14:28
-
1Can you provide more examples? – Apr 12 '18 at 14:28
-
1We'll need more examples in different formats to be able to identify a pattern. – Jacob G. Apr 12 '18 at 14:30
-
btw note that in case the format is not fixed, the time data can include a `.` for microseconds – Tim Apr 12 '18 at 14:32
-
@TimCastelijns yes format is not fixed. Time data can include a '.'. But I need to know what is delimiter for the date part. After thinking again it seems impossible to me! Any idea? – Joseph K. Apr 12 '18 at 14:34
-
1@JosephK. Have you read the other comments? We will not be able to help until you specify either all of the possible formats or enough to generate a pattern. – Jacob G. Apr 12 '18 at 14:37
-
In my app user determines the format. So I have no idea about the format type. – Joseph K. Apr 12 '18 at 14:38
-
@JosephK. Then save the format he's determined and use it later. – M. Prokhorov Apr 12 '18 at 14:39
-
1So just have the user specify the delimiter? – Jacob G. Apr 12 '18 at 14:39
-
You want to try the dozen of existing pattern? – azro Apr 12 '18 at 14:40
-
Do you want to *get* the delimiter, or will it be OK if you can just parse the string even though the delimiter may b `.`, `-` or `/`? – Ole V.V. Apr 12 '18 at 14:47
-
Actually it is a very weird request of my boss. It is hard to explain the reason. In a nutshell user give me a string like that 'yyyy.MM.dd HH:mm'. In runtime date coming to me in this format. So I have a string which hold the defined format and also a string which hold the actual date which is formatted already. I need to know which delimiter used in the date part of datetime format. – Joseph K. Apr 12 '18 at 14:47
-
1Can you be sure there *is* a delimiter? `yyyyMMdd` format has been seen before. Could there be two (or more) different delimiters? In handwriting I use 31/12 2017 or 31/12-2017, that is, slash and space or slash and dash. – Ole V.V. Apr 12 '18 at 15:04
3 Answers
To answer this, we'll have to make a few assumptions:
- As you've said, the delimiter can be
-
,/
, or.
, so I will be assuming no other delimiter is allowed. - From your only example in your question, we're looking for the delimiter of the date and not the time.
- From your example, we'll assume that the date is always listed before the time.
Therefore, we should be able to solve this by looking for the first occurrence of any of those three characters:
public static Optional<Character> findDelimiter(String formattedDate) {
for (char c : formattedDate.toCharArray()) {
switch (c) {
case '.':
case '/':
case '-':
return Optional.of(c);
case ' ':
case '\t':
return Optional.empty();
}
}
return Optional.empty();
}
This will stop at the first instance of a space or a tab, assuming neither of those is the delimiter.

- 28,856
- 5
- 62
- 116
-
In case of a completely unexpected delimiter in the date part it might return a delimiter or decimal point from the time part. I don’t think I’d want to risk that. – Ole V.V. Apr 12 '18 at 14:49
-
@OleV.V. Good point. I suppose we can stop at the first character of whitespace, assuming whitespace isn't a delimiter. – Jacob G. Apr 12 '18 at 14:51
-
1Thanks, at least it is a good answer, upvoted for that. I can use it if could not find any better solution. I am aware my problem is strange, thanks for the efforts in advance. – Joseph K. Apr 12 '18 at 14:53
-
@JosephK. Happy to help! Because the format is created by the user, I don't think any answer would suffice other than having them manually enter their delimiter along with their format. – Jacob G. Apr 12 '18 at 14:57
Basically you have 2 options :
Find the delimiter, and choose how to parse with this infomartion :
private static LocalDateTime stringToDate(String str) { char delimiter = str.charAt(2); switch (delimiter) { case '.': return LocalDateTime.parse(str, DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss")); case '/': return LocalDateTime.parse(str, DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss")); case '-': return LocalDateTime.parse(str, DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss")); default: throw new DateTimeParseException("Bad String", str, 0); } }
Find the delimiter and build the pattern with it :
private static LocalDateTime stringToDate(String str) { String delimiter = str.substring(2, 3); if (delimiter.matches("[-\\.\\/]")) { return LocalDateTime.parse(str, DateTimeFormatter .ofPattern("dd" + delimiter + "MM" + delimiter + "yyyy HH:mm:ss")); } else { throw new DateTimeParseException("Bad String " + str, str, 0); } }
Use :
String strdate1 = "04-03-2018 02:34:22";
LocalDateTime date1 = stringToDate(strdate1);

- 53,056
- 7
- 34
- 70
-
"Format can be different" - I don't believe `charAt(2)` will always work. – Jacob G. Apr 12 '18 at 14:38
-
-
2throwing an exception is not acceptable. Just because your code can't handle the input doesn't mean the sinput is bad – Tim Apr 12 '18 at 15:03
From the format string I would use a regular expression for taking out the longest substring that starts and ends with y
, M
or d
and in between hasn’t got other letters. If more such substrings I’d take them all in turn. This should give you the yyyy-MM-dd
or corresponding part of the format. Then from this substring (these substrings) I would take out all chars that are neither letters nor whitespace (use a regexp again). Or maybe also whitespace. There you’ve got your delimiter/s. Consider the case when they are none or there are more than one.
It may be a bit more complicated than what others have said, but I think it’s also a bit safer if you really don’t know what the format can look like.
There may be a special case: the usual formatters (DateTimeFormatter
and SimpleDateFormat
) allow punctuation to be enclosed in apostrophes. For example yyyy'.'MM'.'dd
means the same as yyyy.MM.dd
. Maybe you want to take it into account. If one really wanted apostrophe as delimiter, it should be doubled: yyyy''MM''dd
.

- 81,772
- 15
- 137
- 161