Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: how to convert any date formats into yyyyMMdd format

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default how to convert any date formats into yyyyMMdd format

    package sample;
     
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Locale;
     
    public class NewClass {
     
    	public static void main(String[] args) {
            String value = "23-04-2012"; 
     
     
    	    String[] formats = { 
     
    	                "yyyy:MM:dd" "yyyyMMdd", "dd-MM-yyyy","dd/MM/yyyy","MM-dd-yyyy","MM/dd/yyyy",};
     
     
     
     
     
    	        if (value != null) {
    	            for (String parse : formats) {
    	                SimpleDateFormat sdf = new SimpleDateFormat(parse);
    	                try {
    	                   sdf.parse(value);
    	                    System.out.println("Printing the value of " + parse);
     
    	                     DateFormat dateFormatNeeded = new SimpleDateFormat("yyyy-MM-dd",Locale.ENGLISH);  
    		            	   dateFormatNeeded.setLenient(false);
    		            	    Date date1 = sdf.parse(value);  
     
     
    		            	    String Dateconverted = dateFormatNeeded.format(date1); 
     
    		            	   System.out.println("Date converted to yyyy-mm-dd"+Dateconverted);	
    	                } catch (ParseException e) {
     
    	                }
    	            }}
     
     
    	        }
    }

    output i obtained:

    Printing the value of yyyyMMdd
    Date converted to yyyy-mm-dd0022-12-04


    Expected Output:

    Printing the value of dd-MM-yyyy
    Date converted to yyyy-mm-dd2012-04-23


  2. #2
    Junior Member
    Join Date
    Jul 2014
    Location
    Canada
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 5 Times in 4 Posts

    Default Re: how to convert any date formats into yyyyMMdd format

    Next time you should probably edit your post rather than starting a new thread.

    That output is because "23-04" in yyyyMMdd is parsed as "the 4th of the month 0 months before year 23", 0022-12-04.

    Your naming conventions are all over the place, so I'm going to assume you've collaged a bunch of code examples together to make this. I'm not sure I understand what you're trying to achieve here, but it looks like you're trying to give the correctly parsed value of any given date in any given format. This is bad form in a bunch of ways.

    First, don't use thrown exceptions as program logic. Think of a way to check the incoming date's format rather than filtering it by throwing ParseExceptions.

    Second, reduce the amount of processing done as much as possible. This program will iterate over your entire format list for every input, which is very inefficient. It may not seem important given the small number of variants, but what if your formats array was much larger? What if you needed to check a large number of incoming requests?

    Third, there's no way to differentiate dd-MM-yyyy from MM-dd-yyyy (02-01-2014, Jan. second or Feb. first?). What is the context of the program? If the input dates are truly unknown, consider where they're coming from. Is it possible to rewrite the program to use Locales?

    As an aside, stick to the Java naming conventions (camelCase for variable names), and try to use descriptive names (NewClass, value, parse, and date1 are not descriptive). Also, try to avoid null values - they force you to add additional logic, and can cause errors that are difficult to track down in larger programs.

Similar Threads

  1. Read Date from Excel and change its Format
    By vector_ever in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 22nd, 2013, 05:13 AM
  2. How to convert date to different format
    By sebE23 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 30th, 2013, 03:44 AM
  3. Java Date Format in Date Object
    By Ashr Raza in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 13th, 2012, 10:47 AM
  4. date format
    By arunjib in forum Java Theory & Questions
    Replies: 4
    Last Post: March 12th, 2011, 01:42 PM
  5. Replies: 2
    Last Post: June 26th, 2009, 11:08 AM