Parse date in String format to Gregorian Calendar, with hour and minute

MrPedru22

I'm trying to parse an input string date to GregorianCalendar:

Scanner scan = new Scanner(System.in);
String next = scan.next("[0-9]{2}.[0-9]{2}.[0-9]{4} [0-9]{2}:[0-9]{2}");
scan.nextLine();

DateFormat df = new SimpleDateFormat("dd.MM.yyyy hh:mm");
Date date = null;
try {
    date = df.parse(next);
} catch (ParseException e1) {
    e1.printStackTrace();
}

GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date); 

System.out.println(cal.getTime());

But it keeps giving this error:

java.util.InputMismatchException
   at java.util.Scanner.throwFor(Unknown Source)
   at java.util.Scanner.next(Unknown Source)
   at java.util.Scanner.next(Unknown Source)

Can anybody help me? This works if i don't specify the hours and minutes but not like this...

nhahtdh

Looking at the source code of Scanner.next(Pattern), which is called by Scanner.next(String), the method roughly follows these steps:

  1. Use the delimiter pattern to skips all the delimiters from the current position in the stream.

  2. Search for the next delimiter in the stream. Blocks if more input is needed to find the delimiter. Continue if end of stream

  3. Check that the pattern matches the token between the current position and the next delimiter (or end of stream).

  4. Return the token if matches. Otherwise, throw Exception.

Since space is matched by the default delimiter pattern ("\\p{javaWhitespace}+"), Scanner tokenizes at the date part and tries to match the token against the pattern and fails.

01.01.2014 00:00
^--------^^
  token   |
          |
      delimiter

One way is to scan them separately:

String nextDate = scan.next("[0-9]{2}\\.[0-9]{2}\\.[0-9]{4}");
String nextTime = scan.next("[0-9]{2}:[0-9]{2}");
String next = nextDate + " " + nextTime;

In my opinion, it is the most simple solution to the problem, which avoids dabbling with the delimiter pattern. I don't know if it is the best solution, though.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Parse String to Date with Different Format in Java

How to get current moment in ISO 8601 format with date, hour, and minute?

Parse date string and change format

Parse date in specific format using Thai Calendar in Android

DateFormatter parse String to Date gives wrong format

How to parse time (hour, minute) from a string?

How to parse JSON format date string into date format

Using boost parse datetime string: With single digit hour format

how can I change Hijri(Islamic Calendar) string to Gregorian date in swift

How to convert string to date in 24 hour format in powershell?

Convert Time Stamp into date, time, hour, minute

Parse and format date in string

Match hour:minute time format with regex in Python

How to format ''date+hour'' string into date time format

How to create a date object from string consisting of Hour, Minute, Date, Month and Year?

Convert date locale in google sheet from Gregorian calendar to Jalali calendar

Parse some string date to another format

How to convert Gregorian string to Gregorian Calendar?

R convert date column into gregorian calendar

Gregorian Calendar Date Confusion

Parse String date to specific format

How to convert a Gregorian calendar date format to Julian date format?

Add 15 minute to 12 hour time format

Unable to parse the string to date format

Gregorian Calendar Wrong Hour

Set date, day, hour, minute, second interval

Insert Hour and Minute to mongoDB date in node js

Date format for year/month/date'T'/hour/minute/second/timezone in Python

How to get the date with specific hour and minute of today from "HH:mm" string in Objecive-C