• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SimpleDateFormat

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Steven Marco
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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;

}
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The case is significant here - while M needs to be uppercase, y and d need to be lowercase.
 
Steven Marco
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Seriously Rick? Seriously? You might as well just read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic