-1

I have below code snippet : java.util.Calendar calendar = java.util.Calendar.getInstance();

curdate = "2025-01-31T12:27:00"  
  def parsedDate = new Date().parse("yyyy-MM-dd'T'HH:mm:ss", curdate)
  calendar.setTime(parsedDate);
  curhour = calendar.get(Calendar.HOUR_OF_DAY);
  curmin = calendar.get(Calendar.MINUTE);

I want to parse curdate "2025-01-31T12:27:00" into parsable format but it is throwing below excpetion :

enter image description here

can anyone plase help me how to parse the date in this format ? this is in groovy script

4
  • Please edit your question to convert images into text, and use a [code block] for any code you have. Readers can then search the text or run the code to reproduce your issue. Commented Feb 10 at 7:25
  • 2
    Which Date class did you import? It doesn't look to me like java.util.Date. Commented Feb 10 at 7:54
  • You can import java.time.LocalDateTime and use its function LocalDateTime.parse(curdate) which will at least work for your example value of curdate because it has the expected format. Commented Feb 10 at 8:40
  • 1
    And then curHour = ldt.toLocalTime().getHour();curMin = ldt.toLocalTime().getMinute(); You should be doing this as java.util.Date has long been superseded Commented Feb 10 at 8:48

2 Answers 2

1

tl;dr

LocalDateTime.parse ( "2025-01-31T12:27:00" ) 

Two Date classes

Java includes two classes named Date, java.util.Date and java.sql.Date. The first represents a moment as seen with an offset from UTC of zero hours-minutes-seconds. The second pretends to represent a date-only value, but actually contains a moment. We cannot tell which you tried to utilize.

Both classes are terribly-flawed, both are legacy, and both should be avoided. So your Question is moot. You should instead be using only the java.time classes for your date-time work. Never use Date, Calendar, SimpleDateFormat, etc.

java.time.LocalDateTime

Neither of those legacy classes is meant to represent the content of your input text. Your input text contains a date with a time-of-day but lacks the context of a time zone or offset. So parse as a LocalDateTime object.

Your input text complies with the standard ISO 8601 used by default in the java.time classes. So no need to specify a formatting pattern.

String input = "2025-01-31T12:27:00" ;
LocalDateTime ldt = LocalDateTime.parse ( input ) ;
Sign up to request clarification or add additional context in comments.

Comments

0

The exception you get means that you must include Groovy Date Time extension to your dependencies.

then you'll be able to write code as simple as:

String curdate = "2025-01-31T12:27:00"  
Date parsed = Date.parse "yyyy-MM-dd'T'HH:mm:ss", curdate
Stirng HHmm = parsed.format 'HH:mm'

If you for whatever reason can not change your deps, then you can either use SimpleDateFormat or other datetime facilities from JDK.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.