wood burning stoves*
The moose likes Beginning Java and the fly likes SimpleDateFormat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "SimpleDateFormat" Watch "SimpleDateFormat" New topic
Author

SimpleDateFormat

Steven Marco
Greenhorn

Joined: Jul 23, 2002
Posts: 21
I would like to write a function that accepts two parameters, one is the date format (4 possible combinations: mm-dd-yyyy, mm/dd/yyyy, dd-mm-yyyy, dd/mm/yyyy), and the other is the date according to the format in the first parameter. For example I can pass in mm/dd/yyyy and 11/13/2006. This method should always return the format in dd-mm-yyy. Can anyone give me some hint? I know I need to use SimpleDateFormat, but I am not sure which method I should use? It should look something like this:


public String formatDate(String pattern, String actualDate)
{

SimpleDateFormat sdf = new SimpleDateFormat(pattern);

Date myDate = sdf.parse(actualDate);

myDate.format(myDate);

return myDate;

}

So if I pass in mm/dd/yyyy and 11/13/2006

The return string should be 13-11-2006
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
For generating the desired output, you need a second SimpleDateFormat object that uses the pattern dd-mm-yyy. That's the one whose format method you'd call.


Ping & DNS - my free Android networking tools app
Steven Marco
Greenhorn

Joined: Jul 23, 2002
Posts: 21
Originally posted by Ulf Dittmer:
For generating the desired output, you need a second SimpleDateFormat object that uses the pattern dd-mm-yyy. That's the one whose format method you'd call.


I tried the following but it gives me an exception at "sdf2.format(myDate)"

public String formatDate(String pattern, String actualDate)
{

SimpleDateFormat sdf = new SimpleDateFormat(pattern);

java.util.date myDate = sdf.parse(actualDate);

SimpleDateFormat sdf2 = new SimpleDateFormat("dd-mm-yyyy");

sdf2.format(myDate);

return myDate;

}
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Be careful though - in SimpleDateFormat, m means minutes, and M means months. Check the API for further details. If you're accepting the format string as an input, do you know that the caller will use the correct format? If there are four possibilities, it may be safer to create all 4 possible SimpleDateFormat objects at startup, then let the caller choose a format by passing an enum or int value, rather than a String. E.g. 1 means format 1, 2 for format 2, etc. An enum is usually preferable, unless you can't use JDK 5.


"I'm not back." - Bill Harding, Twister
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I wrote the last post without seen your followup. Try replacing mm with MM, and if the problem persists, tell us what the exception said, and which line it was thrown from. It might also be useful to know what the input values of pattern and actualDate were.
Steven Marco
Greenhorn

Joined: Jul 23, 2002
Posts: 21
Originally posted by Jim Yingst:
I wrote the last post without seen your followup. Try replacing mm with MM, and if the problem persists, tell us what the exception said, and which line it was thrown from. It might also be useful to know what the input values of pattern and actualDate were.


Assuming the user passes in pattern as "MM-dd-YYYY" and userDate as "11/01/2006", the code below will output "Thur Nov 01 16:18:30 EDT 2006" for systemDate but throws an exception for finalDate and I can't see the output.

public String formatDate(String pattern, String userDate)
{

SimpleDateFormat sdf = new SimpleDateFormat(pattern);
SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MM-YYYY");

try{
java.util.Date systemDate = sdf.parse(userDate);
System.out.println(systemDate);

String finalDate = sdf2.format(systemDate);
System.out.println(finalDate);
}
catch (Exception ex) {
System.out.println(ex);
}

return finalDate;

}
[ November 02, 2006: Message edited by: Steven Marco ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The case is significant here - while M needs to be uppercase, y and d need to be lowercase.
Steven Marco
Greenhorn

Joined: Jul 23, 2002
Posts: 21
Originally posted by Jim Yingst:
The case is significant here - while M needs to be uppercase, y and d need to be lowercase.


Yes, thank you. But now I got another problem, I pass in "dd-MM-yyyy" and "11-16-2006", but the line:

java.util.Date systemDate = sdf.parse(value);

gives me Wed Apr 11 00:00:00 EDT 2007!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well, that's a creative way for it to interpret a month of 16. Surprising. You can use setLentient(false) to force the SDF to be more picky and throw an exception here, which seems more appropriate.
 
GeeCON Prague 2014
 
subject: SimpleDateFormat