• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

Date format issue

private final static SimpleDateFormat SDATEFORMAT =
                  new SimpleDateFormat("MM-dd-yyyy");

public boolean validCache(String ddate)
{
      try
      {
                              
            Calendar cal = Calendar.getInstance();            
            cal.setTime(SDATEFORMAT.parse(SDATEFORMAT.format(cal.getTime())));
            Calendar cal1 = Calendar.getInstance();
            cal1.setTime(SDATEFORMAT.parse(ddate));

            if(( cal.get(Calendar.DATE) == cal1.get(Calendar.DATE)) &&
                  ( cal.get(Calendar.MONTH)+1 == cal1.get(Calendar.MONTH)+1) &&
                  ( cal.get(Calendar.YEAR) == cal1.get(Calendar.YEAR) ))
                  return true;
      }

The ddate coming in is in "yyyy-MM-dd" format causing a problem .How can I convert the String to format it as "MM-dd-yyyy" in Simple DateFormat? or is there any other way to compare dates?
0
skn73
Asked:
skn73
  • 6
  • 3
  • 3
  • +1
1 Solution
 
Bob LearnedCommented:
What is the problem with the unambiguous Y-M-D format?  

ddate = DateTime.Parse(ddate).ToString("MM-dd-yyyy");

Bob
0
 
PreeceCommented:
just a guess, but would it help to try changing the date format:
SimpleDateFormat("MM-dd-yyyy");

to:
SimpleDateFormat("yyyy-MM-dd");


Also, I know in VB you can do something like:
sDate = Format(dtSomeDate, "MM-DD-YYYY")

0
 
skn73Author Commented:
since the ddate is in this format "yyyy-mm-dd" and other date is in "mm-dd-yyyy" format ...

both are picking the different month and day.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
skn73Author Commented:
preece ..
 Thats my first thought .. but if the ddate format changes , then I will be in trouble!
0
 
PreeceCommented:
There must be date conversion methods in C++ (I'm assuming this is in C++, could be C, java, etc...)
0
 
Bob LearnedCommented:
And I assumed C#, but it could be Java :))

Bob
0
 
skn73Author Commented:
This is Java..

what is DateTime in DateTime.Parse(ddate).ToString("MM-dd-yyyy");?
0
 
Bob LearnedCommented:
Those are all C# constructs, and won't help in Java, so never mind :)

Bob
0
 
InteractiveMindCommented:
Preece, it's in Java; you can tell this, because C doesn't have 'String's, and C++ has 'strings' (lower-case 's'); whereas Java has 'String's.

skn73, does this help you: http://www.javaalmanac.com/egs/java.text/FormatDate.html  ?
0
 
Bob LearnedCommented:
C# has strings, too, but as a VB.NET programmer trying to learn about C# (without much knowledge of Java either), I didn't catch that one right away.

Never ASS-U-ME, Never ASS-U-ME... :))


Bob
0
 
InteractiveMindCommented:
> The ddate coming in is in "yyyy-MM-dd" format causing a problem .How can I convert the String to format it as "MM-dd-yyyy"

You can use a String#split() call (assuming you're using JDK1.4+), like so:

String ddate = "2005-05-02";          // yyyy-mm-dd.
String [] tokens = ddate.split("-");
String newDate = tokens[1] + "-" + tokens[2] + "-" + tokens[0];         // This is your new date, in the required format.

0
 
Bob LearnedCommented:
If the date is coming in as Y-M-D, then you could use:

private final static SimpleDateFormat SDATEFORMAT = new SimpleDateFormat("yyyy-MM-dd");

Bob
0
 
InteractiveMindCommented:
As an alternative to the String#split(String) method call (which doesn't work with any JDK lower than 1.4), you can use the StringTokenizer class:

String ddate = "2005-05-02";          // yyyy-mm-dd.
StringTokenizer st1 = new StringTokenizer( ddate, "-" );

String yyyy = st1.nextToken();
String mm = st1.nextToken();
String dd = st1.nextToken();

String newDate = mm + "-" + dd + "-" + yyyy;                 // This is your new date, in the required format.

0
 
Bob LearnedCommented:
Accept Preece (13910799)

Bob
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now