Formato data Java che consente - / o. come separatori entro la data
-
22-07-2019 - |
Domanda
Qual è il modo migliore per analizzare una data che può essere in uno dei seguenti formati
"dd-MM-yyyy HH:mm"
"dd/MM/yyyy HH:mm"
"dd.MM.yyyy HH:mm"
senza creare 3 SimpleDateFormats e analizzandoli su ciascuno di essi.
Grazie
Soluzione
Probabilmente è più facile " modificare " la stringa di origine in un formato canonico:
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);
}
}
Quindi usa la stringa di formato usando " - " ;.
Nota che questo è molto specifico, sostituendo solo esattamente i personaggi che ti interessano, per evitare effetti collaterali indesiderati.
Altri suggerimenti
Potresti prima eseguire due operazioni di sostituzione, in modo da ridurre tutti e tre i formati in uno solo?
Puoi usare 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));
}
}
}
Beh, internamente crea SimpleDateFormats, ma cosa c'è che non va?
che ne dici di una regex:
"\\d\\d[./-]\\d\\d[./-]\\d\\d\\d\\d \\d\\d:\\d\\d"
Nel codice questo significherebbe qualcosa del genere:
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));
}
Fallo da solo con un'espressione regolare:
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);
}
}
}
Si noti tuttavia che ciò consente di miscelare diversi separatori, ad es. "17-09 / 2009 12: 00" sarebbe consentito.
ParseExact può assumere una vasta gamma di formati. Devi ancora specificare tutti i formati, ma è una singola operazione.