3 Ответы Последний ответ: 02.05.2010 14:00, автор: 807580

    getting the custom date format

    user8744860
      Hi
      I am a newbie and got stuck in getting the date from the entered string

      Suppose i enter

      String str="01-02-2010";

      i want to convert this string into date such that as i have string as 01-02-2010 , in similar format i should have my string converted date
      i know the basics of date functions but i am really not getting, tried with SimpleDateFormat
      but not getting the way i want it

      Thanking in advance

      Abhi
        • 1. Re: getting the custom date format
          JoachimSauer
          abhinav291988 wrote:
          tried with SimpleDateFormat
          That's the right direction.
          but not getting the way i want it
          Describe the following:

          1.) what you want to get
          2.) what you tried to do
          3.) what you actually got
          • 2. Re: getting the custom date format
            user8744860
            {
            
            import java.text.*;
            
            import java.util.*;
            
            
            public class DateFormatTest
            
            {
            
              public DateFormatTest()
            
              {
            try
            {
                String dateString = "2010/05/01";
            
                
            
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/M/dd");
            
                Date convertedDate = dateFormat.parse(dateString);
            
            
                System.out.println("Converted date : " + convertedDate);
            
            }catch (ParseException e){} 
             }
            
            
              public static void main(String[] argv) 
            
              {
            
                new DateFormatTest();
            
              }
            
            }
            I tried this and executing this i will get the complete date with time
            like
            converted date : Sat May 01 00:00:00 GMT 2010

            i dont want this
            i want the output as

            converted date : 2010/05/01
            i need it as i need some arithmetic to be done on this date

            please help

            Thankin in advance

            Abhi
            • 3. Re: getting the custom date format
              807580
              You are not using the format method of the SDF object:
              System.out.println("Converted date : " + dateFormat.format(convertedDate))
              And never, never do this, unless you have a very good reason for doing so (you don't):
                  } catch (ParseException e) {
                  }
              Edited by: Encephalopathic on May 2, 2010 6:59 AM