The Artima Developer Community
Sponsored Link

Java Answers Forum
Format a number as Date

2 replies on 1 page. Most recent reply: Sep 23, 2003 9:35 PM by Joe Parks

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Format a number as Date Posted: Sep 23, 2003 9:00 AM
Reply to this message Reply
Advertisement
Hello

I have a problem in SimpleDateFormatter and it will be nice if any one help me.

As we know a user can set any valid format to a SimpleDateFormatter.e.g "mm:dd", "MM/dd/yy" etc.

Now I want that if I have set a format to a SimpleDateFormatter (say "MM/dd:yy")and if I pass a string of numbers say "123456" it should format the string in the specified format.(ie. 12 should be taken as month 34 should be taken as day (can convert 34 to 30+4 or can throw error.))Similar to what MS Excel does when we set format of a cell of MS excel to DateFormat.

Any suggestions, ideas is welcome.


zenykx

Posts: 69
Nickname: zenykx
Registered: May, 2003

Re: Format a number as Date Posted: Sep 23, 2003 3:36 PM
Reply to this message Reply
The SimpleDateFormat class contains a method parse(String, ParsePosition) which returns the formatted String. ParsePosition is only a wrapper class for an int poiting to the index of the character in the String to begin with.

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: Format a number as Date Posted: Sep 23, 2003 9:35 PM
Reply to this message Reply
I think that you will need 2 SimpleDateFormat objects, one to parse the string and one to write it.
SimpleDateFormat inFormat = new SimpleDateFormat("MMddyy");
Date date = inFormat.parse("123456");
SimpleDateFormat outFormat = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(outFormat.format(date));

Flat View: This topic has 2 replies on 1 page
Topic: extended hierarchy!! Previous Topic   Next Topic Topic: please help

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use