Results 1 to 7 of 7
  1. #1
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default problem to convert MM/dd to yyyy-MM-dd

    Dear All,

    I am trying to convert date format from MM/dd to yyyy-MM-dd ..
    input 06/20 ..the output should be 2010-06-20 ..
    but it gives me 1970-06-20..

    what I missed?


    Java Code:
    DateFormat in = new SimpleDateFormat("MM/dd");
    DateFormat out = new SimpleDateFormat("yyyy-MM-dd");
    String dtout = out.format(in.parse("06/20"));

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    I am not sure about the year but did you use try..catch?
    Java Code:
     try{
        DateFormat in = new SimpleDateFormat("MM/dd");
        DateFormat out = new SimpleDateFormat("yyyy-MM-dd");
        String dtout = out.format(in.parse("06/20"));
     }
     catch(Exception){}

  3. #3
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default

    it does not throw any thing..

    the result 1970-06-20

    any idea or comment?

  4. #4
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    i think it gives the minimum year since there is no input of year. you can get the current year anyway...

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Yes.
    The first format has no year so how can the date parser determine what year to use?
    It can't, so it defaults to the start of the epoch (1970).

    If you want to set it to the current year then you'll have to probably append a year to the end of the first string, based on the year returned by a Calendar.getCalendar().getYear().

  6. #6
    kjv007 is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default

    You could do it this way bro, this code assumes that the year is 2010,

    Java Code:
    public static void main(String[] args) {
    		try {
    			String dtout = convertDate2010("06/20");
    			System.out.println(dtout);
    		} catch (ParseException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    	
    	public static String convertDate2010(String oldDate) throws ParseException
    	{
    		DateFormat in = new SimpleDateFormat("MM/dd/yyyy");
    		DateFormat out = new SimpleDateFormat("yyyy-MM-dd");
    		return out.format(in.parse(oldDate+"/2010"));
    	}
    }

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    And how is that different (bar the hard coding of the year) to my suggestion?

Similar Threads

  1. cannot convert from int to int[][]
    By besweeet in forum New To Java
    Replies: 9
    Last Post: 04-18-2010, 03:36 AM
  2. convert XML using XSL
    By rajjan4u in forum XML
    Replies: 3
    Last Post: 11-20-2009, 04:37 PM
  3. I wish to split a string xxx/yyyy/zzz so that ...
    By nmvictor in forum New To Java
    Replies: 10
    Last Post: 10-23-2009, 09:47 AM
  4. Convert to Int
    By 6kyAngel in forum New To Java
    Replies: 0
    Last Post: 03-06-2009, 09:18 AM
  5. how to convert xml to xsd
    By adi in forum XML
    Replies: 2
    Last Post: 02-12-2009, 02:43 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
  •