Results 1 to 7 of 7
  1. #1
    Riyaaz is offline Member
    Join Date
    Jun 2016
    Posts
    3
    Rep Power
    0

    Default Java Date Vaildator

    Hi

    I have a date which needs to be in a certain format.

    I have the format and I have the date but not sure how to check if it's the same.

    So the date looks like this: 2016-06-27 11:26:28 +0200
    The format looks like this: yyyy-MM-dd HH:mm:ss XX

    My method looks like this but when it gets to printing something out, a totally different date comes out.

    Java Code:
    public boolean isThisDateValid(String dateToValidate, String dateFormat){
    		
    		if(dateToValidate == null){
    			return false;
    		}
    		
    		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
                    sdf.setLenient(false);
    		
    		try {
    			
    			//if not valid, it will throw ParseException
    			Date date = sdf.parse(dateToValidate);
    			System.out.println(date);
    		
    		} catch (ParseException e) {
    			
    			e.printStackTrace();
    			return false;
    		}
    		
    		return true;
    	}
    
    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Java Date Vaildator

    Works for me here. Am I missing something?
    Java Date Vaildator-works.png
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Java Date Vaildator

    Java Code:
    System.out.println(date);
    The above line will print out 'date' in the default format for your locale.
    Date objects have no format, they are simply a number.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Riyaaz is offline Member
    Join Date
    Jun 2016
    Posts
    3
    Rep Power
    0

    Default Re: Java Date Vaildator

    Thank you for prompt reply.

    So it is correct in proving that the dates match?

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Java Date Vaildator

    Quote Originally Posted by Riyaaz View Post
    Thank you for prompt reply.

    So it is correct in proving that the dates match?
    In order to match dates, you need two of them. You now only have one and you can't compare one. This method ensures the date given as String is parsable according to the pattern given. That's all. If it can't be parsed, the method will return false.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Java Date Vaildator

    If you want to print out the Date in the format you received it then you need to reverse the process and call format():
    Java Code:
    System.out.println(sdf.format(date));
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Riyaaz is offline Member
    Join Date
    Jun 2016
    Posts
    3
    Rep Power
    0

    Default Re: Java Date Vaildator

    Thank you for the help. Much appreciated.

    I understand now. I didn't fully understand what the method did but i do now.

Similar Threads

  1. Replies: 5
    Last Post: 02-16-2012, 09:26 AM
  2. Java 7 java.util.Date vs java.sql.Date problem
    By pavel_skala in forum Advanced Java
    Replies: 0
    Last Post: 11-08-2011, 11:26 AM
  3. Replies: 4
    Last Post: 07-27-2011, 11:57 AM
  4. java.util.Date vs java.sql.Date
    By Jack in forum New To Java
    Replies: 5
    Last Post: 10-28-2010, 02:59 PM
  5. Creating a Gregorian Calendar using a Date object gives date - 1
    By prachi_goliwadekar in forum New To Java
    Replies: 1
    Last Post: 05-08-2008, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •