I wanted that the program takes input removes the delimiters from that String then add it back then I can later parse it to a LocalDate object but I am not able to do the needful.
Scanner darshit = new Scanner(System.in);
String oo = "";
System.out.println("Enter your DOB: ");
String dob = darshit.next();
String[] words = dob.split("\\D");
for (int i = 0; i > words.length; i++) {
oo = oo + words[i];
}
System.out.println(oo);
After entering the DOB as 25-06-2008, for example, the output should be 25062008 or 2662008 but instead of this, I get a blank line!
Use DateTimeFormatter to parse the input string to LocalDate and then format the LocalDate into a String of the desired format.
Demo:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner darshit = new Scanner(System.in);
System.out.print("Enter your DOB: ");
String dob = darshit.next();
DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("dd[-][/]MM[-][/]uuuu", Locale.ENGLISH);
LocalDate date = LocalDate.parse(dob, dtfInput);
// Output in the default format i.e. LocalDate#toString implementation
// System.out.println(date);
// Output in a custom format
DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("ddMMuuuu", Locale.ENGLISH);
String formatted = dtfOutput.format(date);
System.out.println(formatted);
}
}
Notice the optional patterns in the square bracket which one of the great things about DateTimeFormatter.
A sample run:
Enter your DOB: 25-06-2008
25062008
Another sample run:
Enter your DOB: 25/06/2008
25062008
Learn more about the modern Date-Time API* from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
Can you just use Java Streams with the Collectors joining call ?
String value = Arrays.asList(dob.split("\\D")).stream().collect(Collectors.joining());
String.replaceAll() and DateTimeFormatter
private static final DateTimeFormatter DATE_PARSER
= DateTimeFormatter.ofPattern("[-]d-M-u[-]", Locale.ROOT);
private static final DateTimeFormatter DATE_FORMATTER
= DateTimeFormatter.ofPattern("dd-MM-uuuu", Locale.ROOT);
public static void parseAndFormat(String input) {
String adapted = input.replaceAll("\\W+", "-");
System.out.println("Adapted input string: " + adapted);
LocalDate date = LocalDate.parse(adapted, DATE_PARSER);
String formatted = date.format(DATE_FORMATTER);
System.out.println("Formatted: " + formatted);
}
The above method parses your string:
parseAndFormat("25-06-2008");
Output:
Adapted input string: 25-06-2008
Formatted: 25-06-2008
It’s very tolerant to which delimiters the users decides to use between the numbers, and also before or after them if any:
parseAndFormat("$5///7 2008 ?");
Adapted input string: -5-7-2008-
Formatted: 05-07-2008
How it works: input.replaceAll("\\W+", "-") substitutes any run of non-word characters — everything but letters a through z and digits 0 through 9 — with a single hyphen. No one is interested in seeing the adapted input string, I am only printing it for you to understand the process better. The formatter I use for parsing accepts an optional hyphen first and last. The square brackets in the format pattern denote optional parts. It also accepts day and month in 1 or 2 digits and year in up to 9 digits. I use a separate formatter for formatting so I can control that day and month come in 2 digits and there are no hyphens before nor after.
I myself found the solution
import java.util.Scanner;
import java.time.format.*;
import java.time.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your DOB: ");
String dateOfBirth = scanner.next();
String dateOfBirth1 = dateOfBirth.replaceAll("\\s", "");
String[] dateOfBirthArray = dateOfBirth1.split("[\\s\\-\\.\\'\\?\\,\\_\\#]+");
int[] dateOfBirthArray1 = new int[dateOfBirthArray.length];
for (int i = 0; i < dateOfBirthArray.length; i++){
dateOfBirthArray1[i] = Integer.parseInt(dateOfBirthArray[i]);
}
int dayDateOfBirth = dateOfBirthArray1[0] , monthDateOfBirth = dateOfBirthArray1[1];
int yearDateOfBirth = dateOfBirthArray1[2];
LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(yearDateOfBirth, monthDateOfBirth, dayDateOfBirth);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
String birthday1 = birthday.format(formatter);
}
}
According to me it is the easiest way and the public editors can edit the post for making it more clear
Related
Let's assume a string consists of two parts, day and date.
String arr[] = { "Tuesday 8/11/22", "Monday 15/3/21", "Friday 20/5/21" };
How can I only print out the latter part (date part) on console?
I recommend you use the date-time API instead of performing string manipulation of the elements. The string manipulation will not give you all the benefits that you can get by the specialized date-time API.
Bonus
You get your dates validated free of cost e.g. one of your dates, Friday 20/5/21 is incorrect. This date was Thursday and the java.time API can perform this check for you automatically.
Demo:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.Locale;
class Main {
public static void main(String[] args) {
String arr[] = { "Tuesday 8/11/22", "Monday 15/3/21", "Friday 20/5/21" };
DateTimeFormatter parser = DateTimeFormatter.ofPattern("EEEE d/M/uu", Locale.ENGLISH);
for (String s : arr) {
LocalDate date = LocalDate.parse(s, parser);
// Now you can get individual units from date in a variety of ways
System.out.println(date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH));
System.out.println(date.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.ENGLISH));
System.out.println(date.getYear());
// You can also format it in the desired ways
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/uu", Locale.ENGLISH);
String formatted = date.format(formatter);
System.out.println(formatted);
}
}
}
Output:
Tuesday
Tue
2022
8/11/22
Monday
Mon
2021
15/3/21
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Friday 20/5/21' could not be parsed: Conflict found: Field DayOfWeek 4 differs from DayOfWeek 5 derived from 2021-05-20
at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2023)
Learn more about the modern Date-Time API from Trail: Date Time.
As long as the day and date will stay in that format you can use the split() function:
class DateSplit {
public static void main(String[] args) {
String a = "Tuesday 8/11/22";
String[] result = a.split(" ");
System.out.println(result[1]);
}
}
where the delimiter splitting the string is the whitespace character.
for(String str : arr) {
System.out.println(str.split(" ")[1])
}
public class Main {
public static void main(String[] args) {
String arr[] = { "Tuesday 8/11/22", "Monday 15/3/21", "Friday 20/5/21" };
for(int count = 0; count < arr.length; count++) {
String[] s = arr[count].split(" ");
System.out.println(s[1]);
}
}
}
I'm trying to input a string with a mix of characters and have some bit of code remove all but the part that matches the desired SimpleDateFormat
String wholeString = new String("The time is 7:00.");
String timeOnlyString = CODE TO TRIM STRING;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Date timeAsObject = sdf.parse(timeOnlyString);
String timeAsString = sdf.format(timeAsObject);
System.out.println(timeAsString);`
With this code I'd like
07:00
Printed in the console.
java.time
I recommend that you use java.time, the modern Java date and time API, for your time work.
String wholeString = "The time is 7:00.";
Matcher m = TIME_PATTERN.matcher(wholeString);
while (m.find()) {
String timeOnlyString = m.group();
try {
LocalTime timeAsObject
= LocalTime.parse(timeOnlyString, TIME_FORMATTER);
System.out.println("Time found: " + timeAsObject);
} catch (DateTimeParseException dtpe) {
System.out.println("Looked a bit like a time but couldn’t be parsed as one: " + timeOnlyString);
}
}
I used these two static declarations:
private static final Pattern TIME_PATTERN
= Pattern.compile("\\b\\d{1,2}:\\d{2}\\b");
private static final DateTimeFormatter TIME_FORMATTER
= DateTimeFormatter.ofPattern("H:mm");
Output from my snippet is:
Time found: 07:00
The regular expression that I use for extracting the time from the whole string matches 1 or 2 digits, a colon and 2 digits. It requires a word boundary before and after so we don’t happen to extract a time from 987:12354 or letters1:11moreletters.
The format pattern string used for the DateTimeFormatter has just one H for hour of day. This accepts 1 or 2 digits, so we can parse 15:00 too.
I think that we should take into account that the regex may match more than once in the string, so I am extracting in a loop.
I am parsing in to java.time.LocalTime. This class is for a time of day (from 00:00 through 23:59:59.999999999), so suits your need much better than the outdated Date class (which represents neither a date nor a time of day, but a point in time without time zone).
You can use the regex, .*?(\d{1,2}:\d{1,2}(?:\:\d{1,2}(?:\.\d{1,9})?)?).* to match the whole string and replace it with the pattern matched by group#1.
Description of the regex:
.*? : Matches (lazily) any character any number of times
(: Start of capturing group#1
\d{1,2}:\d{1,2} : One to two digits followed by one to two digits
(?: : Start of the optional non-capturing group
\:\d{1,2} : : followed by one to two digits (for seconds)
(?: : Start of the optional non-capturing group
\.\d{1,9} : . followed by one to nine digits (for nano seconds)
)? : Close of optional non-capturing group
)? : Close of optional non-capturing group
) : Close of capturing group#1
.*: Matches any character any number of times
Demo:
public class Main {
public static void main(String[] args) {
// Test strings
String[] wholeStringArr = { "The time is 7:00.", "The time is 7:00:05.", "The time is 7:00:05.1.",
"The time is 7:00:05.123.", "The time is 7:00:05.123456789." };
for (String wholeString : wholeStringArr) {
String timeOnlyString = wholeString.replaceAll(".*?(\\d{1,2}:\\d{1,2}(?:\\:\\d{1,2}(?:\\.\\d{1,9})?)?).*",
"$1");
System.out.println(timeOnlyString);
}
}
}
Output:
7:00
7:00:05
7:00:05.1
7:00:05.123
7:00:05.123456789
ONLINE DEMO
java.time
The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.
Solution using java.time, the modern Date-Time API:
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String wholeString = "The time is 7:00.";
String timeOnlyString = wholeString.replaceAll(".*?(\\d{1,2}:\\d{1,2}(?:\\:\\d{1,2}(?:\\.\\d{1,9})?)?).*",
"$1");
DateTimeFormatter dtf = DateTimeFormatter
.ofPattern("H:m[:s[.[SSSSSSSSS][SSSSSSSS][SSSSSSS][SSSSSS][SSSSS][SSSS][SSS][SS][S]", Locale.ENGLISH);
LocalTime time = LocalTime.parse(timeOnlyString, dtf);
System.out.println(time);
}
}
Output:
07:00
ONLINE DEMO
Notice the optional patterns in the square brackets.
Learn more about the modern Date-Time API* from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
String wholeString = "The time is 7:00.";
Pattern pattern = Pattern.compile("(0?[1-9]|1[0-2]):[0-5][0-9]");
Matcher matcher = pattern.matcher(wholeString);
if(matcher.find()) {
String timeOnlyString = matcher.group();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Date timeAsObject = sdf.parse(timeOnlyString);
String timeAsString = sdf.format(timeAsObject);
System.out.println(timeAsString);
}
String nDate;
String dateTemp;
int i;
nDate = kb.nextLine();
String[] temp = nDate.split("-");
int numDate = Integer.parseInt(String.valueOf(temp));
im having issues with the (temp) part in the last line. If for example my input is "06-21-2020", what I want to happen is it becomes "06212020"
Use replace() instead of split(). To further explain, String.valueOf() does not take an array of Strings.
String temp = nDate.replace("-","");
int numDate Integer.parseInt(temp);
java.time
The answer by Phaelax z is spot-on for your specific requirement.
However, I recommend you parse the date string to LocalDate and format it as you wish.
You will get much more than the required conversion e.g. just think of finding the name of the day on 06-21-2020 or something like converting to some other format e.g. Sun 21 June 2020. All such requirements can be easily done using the in-built API as shown in the following demo:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String nDate = "06-21-2020";
DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("MM-dd-uuuu", Locale.ENGLISH);
LocalDate date = LocalDate.parse(nDate, dtfInput);
DateTimeFormatter dtfOutput1 = DateTimeFormatter.ofPattern("MMdduuuu", Locale.ENGLISH);
DateTimeFormatter dtfOutput2 = DateTimeFormatter.ofPattern("dd/MM/uuuu", Locale.ENGLISH);
DateTimeFormatter dtfOutput3 = DateTimeFormatter.ofPattern("EEE dd MMM uuuu", Locale.ENGLISH);
System.out.println(dtfOutput1.format(date));
System.out.println(dtfOutput2.format(date));
System.out.println(dtfOutput3.format(date));
}
}
Output:
06212020
21/06/2020
Sun 21 Jun 2020
ONLINE DEMO
Learn more about the modern Date-Time API from Trail: Date Time.
I have a class DayOfWeek which takes a date from the user and prints the day of the week on that date.
For example the user will enter his date as: (with the format MM DD YYYY)
07 22 2016
and the output should be
FRIDAY
This is my class:
public class DayOfWeek{
public static void main(String[] args){
System.out.println("Enter the date (Format: MM DD YYYY) :");
Scanner sc = new Scanner(System.in);
String month = sc.next();
String day = sc.next();
String year = sc.next();
int m = Integer.parseInt(month);
int d = Integer.parseInt(day);
int y = Integer.parseInt(year);
Calendar cal = Calendar.getInstance();
Date date = new Date(y, m, d);
cal.setTime(date);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
System.out.println((sdf.format(dayOfWeek)).toUpperCase());
}
}
However when I enter the date stated above (07 22 2016), I don't get the required output (FRIDAY). Instead the output is the current day of week on my system. I am totally stuck on what the problem is.
You're currently passing an int to SimpleDateFormat... you should pass the Date value. You don't need a Calendar at all for that:
// BAD CODE: DON'T USE - KEEP READING
import java.text.*;
import java.util.*;
public class DayOfWeek {
public static void main(String[] args){
System.out.println("Enter the date (Format: MM DD YYYY) :");
Scanner sc = new Scanner(System.in);
String month = sc.next();
String day = sc.next();
String year = sc.next();
int m = Integer.parseInt(month);
int d = Integer.parseInt(day);
int y = Integer.parseInt(year);
// Note the changes here
Date date = new Date(y - 1900, m - 1, d);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
System.out.println(sdf.format(date).toUpperCase());
}
}
I've put the warning there because you're using a deprecated Date constructor - and it's deprecated for good reasons. Basically, java.util.Date is a terrible API and you should avoid using it if you possibly can. Your original call to new Date(y, m, d) was broken because the first parameter to java.util.Date(int, int, int) represents the year with a 1900 base (so a value of 117 means year 2017) and the second parameter represents the month with a 0 base (so a value of 6 means July). Then there's the implicit time zone conversion, which you really don't want.
You'd be much better off using java.time, and in this case the LocalDate class:
import java.time.*;
import java.time.format.*;
import java.util.*;
public class DayOfWeek {
public static void main(String[] args){
System.out.println("Enter the date (Format: MM DD YYYY) :");
Scanner sc = new Scanner(System.in);
String month = sc.next();
String day = sc.next();
String year = sc.next();
int m = Integer.parseInt(month);
int d = Integer.parseInt(day);
int y = Integer.parseInt(year);
LocalDate date = LocalDate.of(y, m, d);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE");
System.out.println(formatter.format(date).toUpperCase());
}
}
Finally, your approach to parsing the user input isn't ideal either - I'd suggest using a DateTimeFormatter for that, too:
import java.time.*;
import java.time.format.*;
public class DayOfWeek {
public static void main(String[] args){
System.out.println("Enter the date (Format: MM DD YYYY) :");
DateTimeFormatter parser = DateTimeFormatter.ofPattern("MM dd yyyy");
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
LocalDate date = LocalDate.parse(line, parser);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE");
System.out.println(formatter.format(date).toUpperCase());
}
}
Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
if you need the output to be Tue rather than 3 (Days of week are indexed starting at (1), instead of going through a calendar, just reformat the string: new SimpleDateFormat("EE").format(date) (EE meaning "day of week, short version").
Note
As pointed by Jon Try and use the latest Date Time Formatter Api for this purpose the Date Api is depricated .
If you have your input as string, rather than Date, you should use SimpleDateFormat to parse it
: new SimpleDateFormat("dd/M/yyyy").parse(dateString)
#JonSkeet's answer is correct and already explains all the problems of the old API and how to use the new one. I'd just like to add another approach with java.time classes.
If you want just to get a String with the day of the week, you can skip the creation of a LocalDate and get directly a java.time.DayOfWeek.
The first part is the same (get the input from the Scanner and create a DateTimeFormatter to parse it).
Then I create a java.time.DayOfWeek from the parsed input, using the from method. So I call getDisplayName, using java.time.format.TextStyle (which is equivalent to EEEE format in #JonSkeet's answer) and a java.util.Locale to make it explicit that I want the day of week's names in English.
// read line from input
Scanner sc = new Scanner(System.in);
// get "07 22 2016" from the scanner
String line = sc.nextLine();
// formatter to parse the input
DateTimeFormatter parser = DateTimeFormatter.ofPattern("MM dd yyyy");
// get the day of the week
DayOfWeek dow = DayOfWeek.from(parser.parse(line));
// get day of week's name
String output = dow.getDisplayName(TextStyle.FULL, Locale.ENGLISH).toUpperCase();
The value of output variable will be FRIDAY - note that I also called toUpperCase(), because the result of getDisplayName is Friday.
The Locale is also important. If you don't specify a locale, it'll use the system's default, and it's not guaranteed to always be English. And it can also be changed without notice, even at runtime, so it's better to make it explicit in your code.
Notes:
The code above will also work if you want to change the language (just use a different Locale). But if you always want an English uppercase name, you can optionally call dow.toString(), which will also return FRIDAY as well.
This code focus only on the day of the week. If you need to use the date for other things than getting FRIDAY (like checking the day, month, etc), then you'll need a LocalDate and #JonSkeet's solution is preferred (and LocalDate has the method getDayOfWeek(), which returns the corresponding DayOfWeek, just in case you need it).
java.time is available only in Java >= 8. If you're using Java <= 7, though, you can use the ThreeTen Backport. The only difference is the package name (org.threeten.bp instead of java.time), but the classes and methods names are the same.
What's the nicest way to parse a date that can be in one of the following formats
"dd-MM-yyyy HH:mm"
"dd/MM/yyyy HH:mm"
"dd.MM.yyyy HH:mm"
without creating 3 SimpleDateFormats and parsing against each one.
Thanks
It's probably easiest to "tweak" the source string into a canonical format:
if (text.length() == 16)
{
if ((text.charAt(2) == '/' && text.charAt(5) == '/') ||
(text.charAt(2) == '.' && text.charAt(5) == '.'))
{
text = text.substring(0, 2) + "-" + text.substring(3, 5)
+ "-" + text.substring(6);
}
}
Then use the format string using "-".
Note that this is very specific, only replacing exactly the characters you're interested in, to avoid unwanted side-effects.
Could you run two replace operations first, so that you reduce all three formats to a single one?
You may use Apache commons lang DateUtils.parseDate
import java.text.ParseException;
import org.apache.commons.lang.time.DateUtils;
public class Test {
public static void main(String[] args) throws ParseException {
String[] values = new String[]{"31-12-2009 12:00", "31/12/2009 12:00", "31.12.2009 12:00"};
String[] parsePatterns = new String[]{"dd-MM-yyyy HH:mm", "dd/MM/yyyy HH:mm", "dd.MM.yyyy HH:mm"};
for (String value : values) {
System.out.println(DateUtils.parseDate(value, parsePatterns));
}
}
}
Well, internally it creates SimpleDateFormats, but whats wrong with that?
java.time
You can do it using DateTimeFormatter which allows you to specify optional things using square brackets.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.stream.Stream;
public class Main {
public static void main(String args[]) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("d[-][.][/]M[-][.][/]u H:m", Locale.ENGLISH);
// Test
Stream.of(
"28-04-2021 14:01",
"28.04.2021 14:01",
"28/04/2021 14:01"
).forEach(s -> System.out.println(LocalDateTime.parse(s, dtf)));
}
}
Output:
2021-04-28T14:01
2021-04-28T14:01
2021-04-28T14:01
Learn more about the modern date-time API from Trail: Date Time.
Note that the legacy date-time API (java.util date-time types and their formatting API, SimpleDateFormat) are outdated and error-prone. It is recommended to stop using them completely and switch to java.time, the modern date-time API* .
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
how about a regex:
"\\d\\d[./-]\\d\\d[./-]\\d\\d\\d\\d \\d\\d:\\d\\d"
In code this would mean something like this:
Pattern pattern =
Pattern.compile("(\\d\\d)([./-])(\\d\\d)([./-])(\\d\\d\\d\\d) (\\d\\d):(\\d\\d)");
Matcher matcher =
pattern.matcher("31-07-1983 15:30");
if (matcher.find() && matcher.group(2).equals(matcher.group(4))) {
int day = Integer.parseInt(matcher.group(1));
int month = Integer.parseInt(matcher.group(3));
int year = Integer.parseInt(matcher.group(5));
int hour = Integer.parseInt(matcher.group(6));
int minute = Integer.parseInt(matcher.group(7));
}
Doing it yourself with a regular expression:
public class SpecialDateFormat
{
private final static Pattern PATTERN = Pattern.compile("(\\d{2})[\\.\\/\\-](\\d{2})[\\.\\/\\-](\\d{4}) (\\d{2}):(\\d{2})");
public static Date parse(String text) throws ParseException {
Matcher m = PATTERN.matcher(text);
if (m.matches()) {
int dd = Integer.parseInt(m.group(1));
int mm = Integer.parseInt(m.group(2));
int yy = Integer.parseInt(m.group(3));
int hh = Integer.parseInt(m.group(4));
int mi = Integer.parseInt(m.group(5));
// NOTE: Checking if values are in valid ranges omitted
Calendar cal = Calendar.getInstance();
cal.set(yy, mm - 1, dd, hh, mi, 0);
return cal.getTime();
}
else {
throw new ParseException("Unparseable date: " + text, 0);
}
}
}
Note however that this does allow mixing different separators, e.g. "17-09/2009 12:00" would be allowed.
ParseExact can take an array of formats. You still have to specify all formats, but it's a single operation.