Date


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Date

  1. #1
    Pelle Guest

    Date


    how do you convert a string to a date ?

  2. #2
    John Timney (MVP) Guest

    Re: Date

    heres one way, bound to be a better version around though.


    import java.text.*;
    import java.util.*;

    public class dformat {
    public static void main (String a[]){
    try {
    /*
    ** we specify Locale.US since months are in english
    ** we want to parse a TimeStamp
    */
    SimpleDateFormat sdf = new SimpleDateFormat
    ("dd-MMM-yyyy HH:mm:ss", Locale.US);
    /*
    ** fix timezone in the SimpleDateFormat
    ** bug in JDK1.1
    */
    sdf.setCalendar(Calendar.getInstance());
    /*
    ** create a Date (no choice, parse returns a Date object)
    */
    Date d = sdf.parse("24-Feb-1998 17:39:35");
    System.out.println(d.toString());
    /*
    ** create a GregorianCalendar from a Date object
    */
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(d);
    System.out.println(gc.getTime().toString());
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    }
    }





    --
    Regards

    John Timney
    Microsoft MVP
    (http://support.microsoft.com/support/mvp/program.asp)
    Co-Author Professional JSP
    ISBN: 1-861003-62-5



    Pelle <pehen@wmdata.com> wrote in message news:39f44fc0$1@news.devx.com...

    how do you convert a string to a date ?



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center