Skip to content

Instantly share code, notes, and snippets.

@emschwar
Last active July 31, 2018 01:39
Show Gist options
  • Save emschwar/63e99a2233dc63c379adfc61288b7ef0 to your computer and use it in GitHub Desktop.
Save emschwar/63e99a2233dc63c379adfc61288b7ef0 to your computer and use it in GitHub Desktop.
Tests how to parse LocalDateTime with DateTimeFormatter
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateTimeFormatterExample {
public static void main(String []args) {
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime time = LocalDateTime.parse("2018-07-30T18:15:34.823", pattern);
System.out.println("parsed time: " + time.toString());
}
}
parsed time: 2018-07-30T18:15:34.823
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexpExample {
public static void main(String []args) {
String timeString = "2018-07-30T18:15:34.823";
String pattern = "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.\\d{3}";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(timeString);
if (m.matches()) {
System.out.println("It matches");
} else {
System.out.println("It doesn't match");
}
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexpExampleWithDateTimeFormatterPattern {
public static void main(String []args) {
String timeString = "2018-07-30T18:15:34.823";
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(timeString);
if (m.matches()) {
System.out.println("It matches");
} else {
System.out.println("It doesn't match");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment