A logo showing the text blog.marcnuri.com
Español
Home»Java»SimpleDateFormat to check user date input // parsing String to dates in java

Recent Posts

  • Fabric8 Kubernetes Client 6.5.0 is now available!
  • Eclipse JKube 1.11 is now available!
  • Fabric8 Kubernetes Client 6.4.1 is now available!
  • I bought an iPad
  • Three years at Red Hat

Categories

  • Front-end
  • Java
  • JavaScript
  • Legacy
  • Operations
  • Personal
  • Pet projects
  • Tools

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • December 2019
  • October 2019
  • September 2019
  • July 2019
  • March 2019
  • November 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • December 2017
  • July 2017
  • December 2015
  • November 2015
  • November 2008
  • November 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007

SimpleDateFormat to check user date input // parsing String to dates in java

2007-05-21 in Java / Legacy tagged Java / Parsing / SimpleDateFormat by Marc Nuri | Last updated: 2021-02-07

When developing user interfaces for management software you usually need to check and transform what the user inputs in order to store the values in a database or a file. Java offers different alternatives to parse a String (usually the way the user inputs values to the system) to other data types.

The class SimpleDateFormat from the java.text package offers a simple method to do this. You just have to call the parse(String yourDate) method in order to get a java.util.Date instance.

Following you can find some code that explains this method and some of its particularities:

1// We create the SimpleDateFormat object with the desired pattern
2SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
3// This is very important. If you need strict parsing,
4// (i.e. January * will only have 31 days you must set this to false.
5// If you don't do this when you parse "32/01/2007" the date you'll
6// you'll get really be 01/02/2007
7sdf.setLenient(false);
8try {
9  System.out.println(sdf.parse("25/12/2022"));
10} catch (ParseException ex) {
11  ex.printStackTrace();
12}
13// This first parse has no problem, when run you will get
14// Sun Dec 25 00:00:00 CET 2022
15// in the console
16try {
17  System.out.println(sdf.parse("34/15/2002"));
18} catch (ParseException ex) {
19  ex.printStackTrace();
20}
21// In this case, the date is wrong thus we will get the corresponding
22// exception
23try {
24  System.out.println(sdf.parse("28/02/202"));
25} catch (ParseException ex) {
26  ex.printStackTrace();
27}
28// This time we will not get an exception, but in some cases we would like to get the exception,
29// because this date although valid, has an uncommon length and may be due to an input error.
30//
31// To solve the above problem, we could create the following function:
32public static Date parseDate(String date) throws ParseException{
33  SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
34  sdf.setLenient(false);
35  if(date.length() != 10){
36    throw new ParseException("Date with wrong length: "+date, 0);
37  } 
38  return sdf.parse(date);
39}
Twitter iconFacebook iconLinkedIn iconPinterest iconEmail icon

Post navigation

← JTable, detecting selection changes // ListSelectionListener /*Selection Changed Event*/Numbers to Strings with custom symbols // DecimalFormat - DecimalFormatSymbols →
© 2007 - 2023 Marc Nuri