DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
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 ?



Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links