Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date format

Status
Not open for further replies.

Iby

Programmer
Jul 17, 2002
18
0
0
IT
Hello,

Could you please help to me because I would like to use this date format "dd/MM/yy" but I don't know how can I handle.
The "@label" field contains the value.

Thank you in advance!
 
That method of my own might help you

public static String dateToString (java.sql.Date aDate,String format) {
java.util.GregorianCalendar takvim=new java.util.GregorianCalendar();
takvim.setTime(aDate);
int iDay,iMonth,iYear; //integer type date part variables
String sDay=""; //day part of date
String sMonth=""; //month part of date
String sYear=""; //year part of date

iDay=takvim.get(takvim.DATE); //get day part of given date
sDay=String.valueOf(iDay); //convert it to string

iMonth=takvim.get(takvim.MONTH); //get month part of given date
sMonth=String.valueOf(iMonth+1); //convert it to string

iYear=takvim.get(takvim.YEAR); //get year part of given date
sYear=String.valueOf(iYear); //convert it to string

//check format of the textbox and
if (format=="dd/mm/yy")
{
if (sDay.length()<2) sDay=&quot;0&quot;+ sDay;
if (sMonth.length()<2) sMonth=&quot;0&quot;+ sMonth;
if (sYear.length()>2) sYear=sYear.substring(sYear.length()-2,sYear.length());
return sDay + &quot;/&quot; + sMonth + &quot;/&quot; + sYear;
}

if (format==&quot;dd/mm/yyyy&quot;)
{
if (sDay.length()<2) sDay=&quot;0&quot;+ sDay;
if (sMonth.length()<2) sMonth=&quot;0&quot;+ sMonth;
if (sYear.length()<4)
{
return sDay + &quot;/&quot; + sMonth + &quot;/&quot; + String.valueOf(1900+Integer.valueOf(sYear).intValue());
}
else
{
return sDay + &quot;/&quot; + sMonth + &quot;/&quot; + sYear;
}
}

if (format==&quot;d/m/yy&quot;)
{
if (sYear.length()>2) sYear=sYear.substring(sYear.length()-2,sYear.length());
return sDay + &quot;/&quot; + sMonth + &quot;/&quot; + sYear;
}

if (format==&quot;d/m/yyyy&quot;)
{
if (sYear.length()<4)
{
return sDay + &quot;/&quot; + sMonth + &quot;/&quot; + String.valueOf(1900+Integer.valueOf(sYear).intValue());
}
else
{
return sDay + &quot;/&quot; + sMonth + &quot;/&quot; + sYear;
}
}
else
{
return null;
}
} Salih Sipahi
Software Engineer.
City of Istanbul Turkey
openyourmind77@yahoo.com
 
Use java.text.SimpleDateFormat:
Code:
// Dates.java
package com.nkl;
import java.util.Date;
import java.text.Format;
import java.text.SimpleDateFormat;
public class Dates {
  public static void main(String[] args) {
    Date today = new Date();
    String[] s = {
      &quot;dd/MM/yy&quot;,
      &quot;dd-MMMM-yyyy&quot;
    };
    Format f;
    for(int i = 0; i < s.length ; i++) {
      f = new SimpleDateFormat(s[i]);
      System.out.println(f.format(today));
    }
  }
}
Cheers, Neil :)
 
Any String comparison that looks like this is always going to be buggy:
Code:
format==&quot;dd/mm/yy&quot;

Does your code even work correctly? Because based on all your comparisons it is not going to work. It is always going to return null.

When dealing with objects you need to always make sure you use the equals() method not ==. Plus you should place the static value first in the comparison for greater type checking and to avoid the possibility of a NullPointerException.

The comparsion should look like:
Code:
&quot;dd/mm/yy&quot;.equals(format)
 
Wushutwist:
Yes you are right the best way is to use equals sign,in fact I didn't notice this syntax.I have been using it for more than three years in my project and it works.Strange isn't it.
But yes you are right equals MUST be the method to be used.Because String is a class not a primitive type.

Thank you for response. Salih Sipahi
Software Engineer.
City of Istanbul Turkey
openyourmind77@yahoo.com
 
Since Java interns String it is possible in certain cases for == to function the same as .equals() on Strings. It is not something that you can rely on though, because if you explicitly create a new String then it will not use the interned copy in memory.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top