Results 1 to 7 of 7
- 08-25-2010, 07:46 AM #1
Senior Member
- Join Date
- Jan 2010
- Posts
- 138
- Rep Power
- 0
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"));
- 08-25-2010, 08:30 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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){}
- 08-25-2010, 08:37 AM #3
Senior Member
- Join Date
- Jan 2010
- Posts
- 138
- Rep Power
- 0
it does not throw any thing..
the result 1970-06-20
any idea or comment?
- 08-25-2010, 08:40 AM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
i think it gives the minimum year since there is no input of year. you can get the current year anyway...
- 08-25-2010, 09:28 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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().
- 08-30-2010, 06:32 AM #6
Member
- Join Date
- Aug 2010
- Posts
- 3
- Rep Power
- 0
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")); } }
- 08-31-2010, 09:39 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
cannot convert from int to int[][]
By besweeet in forum New To JavaReplies: 9Last Post: 04-18-2010, 03:36 AM -
convert XML using XSL
By rajjan4u in forum XMLReplies: 3Last Post: 11-20-2009, 04:37 PM -
I wish to split a string xxx/yyyy/zzz so that ...
By nmvictor in forum New To JavaReplies: 10Last Post: 10-23-2009, 09:47 AM -
Convert to Int
By 6kyAngel in forum Java TipReplies: 0Last Post: 03-06-2009, 09:18 AM -
how to convert xml to xsd
By adi in forum XMLReplies: 2Last Post: 02-12-2009, 02:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks