Converting a String to Date

I have a String in which i am storing the date which I have retrieved from database. How can I convert this String to a Date object and use Comparator for sorting.
String strCreatedDate1 = ((CVendorEmployees) vendoremployee1).getCreatedDate().toUpperCase();The date format which I will retrieve from String is mm/dd/yyyy.
I want to convert this into date and use it for sorting.
Thanx.

Date dtTmp = new
SimpleDateFormat("dd-MM-yyyy").parse(((CVendorEmployee
s) vendoremployee1).getCreatedDate());
                              Calendar calendar = Calendar.getInstance();      
                              calendar.setTime(dtTmp);
Now you have the date object do what ever you wish toBetter make thatnew SimpleDateFormat("MM/dd/yyyy")And no need for the Calendar stuff if all you want is a Date object.

Similar Messages

  • How to convert a string to date and then compare it with todays date???

    Hello.
    I want to set a format first for my dates
    DateFormate df = new SimpleDateFormate("yyyy-mm-dd");
    once this is done then I want to convert any string to date object in the above formate
    String str="2001-07-19";
    Date d = null;
    try{
    d = df.parse(s);
    }catch(ParseException pe) {
    pe.printStackTrace();
    First of all there is something wrong above,cus what I get for this is
    Fri Jan 19 00:07:00 MST 2001
    where as it should have been
    2001-07-19... to my understanding.
    once this part is done I need to get current date in the above set format and compare the
    current date and the date I set.
    I will appreciate the help.
    Thanks

    for the output part:
    a date is a point in time
    the output depends on the format you specify for output
    using for example a SimpleDateFormat.
    You only specified the format for parsing (which is independent for that of output) so java uses some default format ... see the DateFormat.format() method for details.
    for the comparison stuff, I just posted a little code snippet in this forum a few minutes ago.
    the hint is: Date.getTime() returns milliseconds after a fixed date
    hth Spieler

  • Converting a string into Date format

    I am currently using a JSP.
    I currently need to convert a string that holds a date in "dd/mm/yyyy" format to the jva.sql.Date format.
    Any ideas as to what the proper way of doing this is?
    Many thanks ppl

    public java.sql.Date parseDate(String s) {
         java.text.DateFormat dateFormatter= new java.text.SimpleDateFormat("dd/MM/yyyy");
         return new java.sql.Date(dateFormatter.parse(s).getTime());

  • Problem in converting the String to Date with time zone GMT

    Hi,
    When I tried to convert the string 12/05/2009 to Date, the time zone is set to BST.On the other hand, for the date 12/12/2009, the time zone is set to GMT. What should I do to get the time zone as GMT all the time.?
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    String dateString = "12/05/2009";
    System.out.println(myDate.toString());

    I think you are all missing the point. java.util.Date objects always alway always store the date as the number of milliseconds since 1/1/1970 UTC so the only TimeZone they have its the implicit UTC. When you use the Date.toString() method the toString() method gets the default time zone from your environment and formats the data accordingly. This means that the same Date object will, by default, produce a different result for France and Australia and the US.
    So, if you have the date "12/5/2009" as a String then to convert it to a java.util.Date you must specify what TimeZone is implied. If it is your system time zone then you can just create a SimpleDateFormat object with the correct format and then use the parse() method to create the java.util.Date object and this will automatically be converted to UTC. If the date String represents some other time zone then you must explicitly set the time zone of the SimpleDateFormat object before parsing the string.
    The same approach applies when converting a java.util.Date object to a String. If you want anything other than your system time zone then you must explicitly tell the SimpleDateFormat what time zone you want the result formatted for.

  • How to convert a String to Date format?

    the user enter a date in string format and the date is save in the database.
    The problem i am facing is i want to change from String to Date format.
    Here is my codes:
            public boolean insertData() throws Exception {
            boolean validFlag = false;
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date d = df.parse("05/18/05");
            String MY_DATE_FORMAT = "yyyy-MM-dd";
            String jobAdvertisementDate = new SimpleDateFormat(MY_DATE_FORMAT).format(d);
            String sql = "INSERT INTO companyjob (CompanyID,JobID, JobAdvertisementDate ) " +
                    " VALUES ('" + companyID + "','" + jobID + "', '" + jobAdvertisementDate + "')";
            System.out.println(sql);
            validFlag = executeSQL(sql);
            return validFlag;
        }The date is save under jobAdvertisementDate.
    My netbeans shows an error. There's a red line under DateFormat and parse inside my codes.
    PLease help me. Reply asap!!

    BebeGirl wrote:
    My netbeans shows an error. There's a red line under DateFormat and parse inside my codes.
    Red Line? Sounds ominous. I wonder what it means.
    So...what does the error say?

  • Convert from String to Date for storing in SQL Server 2000

    Hi,
    I've accepted some values from a user using a form in HTML.Now using Servlets I transfer the value to my java code .
    I want to know how can I convert a DATE accepted from the user thats presently in "String" format to the "datetime" format for SQL Server2000.
    Please guide me with some steps and I shall be grateful.
    Thanks

    The java.text.SImpleDateFormat class is most probably of use.
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    String enteredDate = "25/12/2006";
    java.util.Date utilDate = sdf.parse(enteredDate);
    java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());You can then use the java.sql.Date with the "setDate()" method of a prepared statement. This method would be database independant, as you are setting an actual date, not depending on a specific format on the database end.

  • Convert a string to date, so that i can insert it on SQL database

    Hi, me again..
    i have this variable
    date.getText()
    i want to convert it to date, so i did this:
    d = (Date) DateFormat.parse(date.getText());
    but it says "Cannot make a static reference to the non-static method parse(String) from the type DateFormat"
    what the hell is that?
    can someone help? i want the date to correspond to the variable format on my database (DATETIME (yyyy-mm-dd))

    GMX750 wrote:
    OK, like this?:
    DateFormat df;
    df = DateFormat.parse(data.getText());
    i did this, but the IDE says the same thing
    : \If you don't know the difference between static and non-static methods, or how to instantiate an object, you're not ready to be messing with JDBC yet. Sorry if that sounds harsh, but it's the truth. If you keep trying to go down this path without getting a handle on the fundamentals, you're just setting yourself up for more frustration and a poor excuse for an education.
    Sun's [basic Java tutorial|http://java.sun.com/docs/books/tutorial/]
    Sun's [New To Java Center|http://java.sun.com/learning/new2java/index.html].Includes an overview of what Java is, instructions for setting up Java, an intro to programming (that includes links to the above tutorial or to parts of it), quizzes, a list of resources, and info on certification and courses.
    [http://javaalmanac.com|http://javaalmanac.com]. A couple dozen code examples that supplement [The Java Developers Almanac|http://www.amazon.com/exec/obidos/tg/detail/-/0201752808?v=glance].
    jGuru. A general Java resource site. Includes FAQs, forums, courses, more.
    JavaRanch. To quote the tagline on their homepage: "a friendly place for Java greenhorns." FAQs, forums (moderated, I believe), sample code, all kinds of goodies for newbies. From what I've heard, they live up to the "friendly" claim.
    Bruce Eckel's [Thinking in Java|http://mindview.net/Books/DownloadSites] (Available online.)
    Joshua Bloch's [Effective Java|http://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683/ref=pd_bbs_1?ie=UTF8&s=books&qid=1214349768&sr=8-1]
    Bert Bates and Kathy Sierra's [Head First Java|http://www.amazon.com/exec/obidos/tg/detail/-/0596004656?v=glance].
    James Gosling's [The Java Programming Language|http://www.bookpool.com/sm/0321349806].

  • How to convert a String to Date?

    i have a String "1998-12-31" i would like to convert it to Date so that i can compare the Date in Mysql.
    i am appreciate for your help.
    Thanks
    yuetni Swee

    hi,
    i try to change the code by using the code below do you think it is ok
    i just use the Date.valueOf(String s) method. is it still working in sql?
              java.sql.Date PickDat = java.sql.Date.valueOf(PDate);
              java.sql.Date DropDat = java.sql.Date.valueOf(DDate);
              System.out.println("IN CARINFO FUNC." + pickdropPnt + PDate + DDate + carclass);
                   sql.append("SELECT * FROM CARS");
                   sql.append("WHERE TO_DAYS('" PickDat "') BETWEEN TO_DAYS('FROM_D') AND TO_DAYS('UNTIL_D') ");
                   sql.append("AND TO_DAYS('" DropDat "') BETWEEN TO_DAYS('"+PickDat+"') AND TO_DAYS('UNTIL_D') ");
                   sql.append("AND PICK_UP_DROP ='" + pickdropPnt +"' ");
                   sql.append("AND CAR_TYPE ='"+carclass + "'");

  • How to convert a string to date object?

    I have a string user input for date.
    I want to convert it to Date object to insert it in database.
    How to do it?

    Check the java.text.SimpleDateFormat class. You can use it for parsing dates. API contains good description how to build the format pattern.
    HTH
    Mike

  • Convert from String to Date

    Hello,
    I have the following string: "Thu Apr 22 00:00:00 IDT 2004"
    and I need to convert it to Date object.
    I tried to use Date.parse() and Date.valueOf(),
    but then I get the IllegalArgumentException.
    How can I do this?
    Thanks.

    split ur string. get year,month,day in int
    time in long
    now it will work for u
         Date d = new Date(int year,int mon,int date);//Deprecated
         d.setTime(long time); //Sets this Date object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT.
         System.out.println(d);
    Ignore that answer, please and DynaFest read the API!

  • I need help to convert a string to date and time

    Good aft,
    1.How can i get a date from the user input thro' Frames?
    2.How to parse it to the date object(I need to include java.util.Date but for the database i need to include java.sql.*,if i do so, i have error as
    found:java.util.Date
    Required:java.sql.Date)
    3.How to insert into the database
    The same i need help to insert time into database?
    I need solution and explanation...
    Reply me immediately...
    If u send me the code, its better to me.

    Hello,
    The best way is to use the HEX to decimal coversion VI in the string pallette. Then, write this decimal directly to your chart. If you are doing it a point at a time put it into a while loop.
    Doug

  • How to convert a string to date using OLEDB function?

    Is there any function similar to OCIDateFromText()?
    I have to read the date from a text file and store it in the sql database in a date column.
    Please suggest how to do it?
    Thanks in Advance.

    In addition, SQL Server 'likes' YYYYMMDD format to operate with dates.
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • Convert from String data type to Character data type

    Hi...,
    I'm a beginner. I try to make a program. But I don't know how to convert from string to data type. I try to make a calculator with GUI window. And when somebody put "+" to one of the window, I just want to convert it to Character instead of String.
    Sorry of my bad english..
    Please help me....
    Thanks

    String s = "a+b";
    char theplus = s.charAt(1);

  • Convert a string to JAva.sql.date..

    I am having a string with the value as follows..
    String fval="03-10-2001"..
    I am using this value in a rs.updateDate method where I need to convert it to a different format as
    2001-10-03...can anybody give me the syntax..

    You should hold dates as Date instances. But if you want to convert between Strings and Dates then you should use a DateFormat:
    String originalDateString = "31-10-2001";
    // Convert the original string to a date
    DateFormat originalDateFormat = new SimpleDateFormat("dd-MM-yyyy");
    Date date = originalDateFormat.parse(originalDateString);
    // Get a representation of the date in the new format
    DateFormat newDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    String newDateString = newDateFormat.format(date);(Excuse the quirky formatting that the Java code filter applies!)
    Hope this helps.

  • Converting String to Date

    Hi All,
    I am having a String value which is having date in the format 'Mon Apr 14 16:07:52 IST 2008'
    Now I want to convert this String to Date with format 'mm/dd/yyyy' , to display as 04/14/2008
    Also I want to add 5 days to this date and display the new date 04/19/2008
    I don't know How to convert the string which is having date in 'Mon Apr 14 16:07:52 IST 2008' format to format I need.
    Please help me.

    Hi ,
    I wrote a class that tries to parse a String Date and converts it to object. However, I found a very interesting thing while testing this code. I was trying to paarse "2008:03:04 16:21:05" string by using
    DateFormat.getDateFromString(null,"2008:03:04 16:21:05" )
    It should by pass all try loop accept the one with yyyy:MM:dd HH:mm:ss format. But it went inside the try loop which had MM:dd:yyyy HH:mm:ss format and gave me a wrong date object with year 0171, april 03.
    Could any one help me to understand why it is happening? as a work around I put MM:dd:yyyy HH:mm:ss at the end of the program and it is giving me correct object as it did not have to go that far before matching the format.
    Thanks,
    AL-Amin
    Code:
    import java.text.*;
    import java.util.*;
    public class DateFormat
    public Date getDateFromString(String format, String parseDate)
         Date d;
         if(format!=null) {
    try
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    d = sdf.parse(parseDate);
    catch(Exception e)
    System.out.println(e.toString());
    d = null;
    return d;
         else {
              try
                   format = "yyyy-MM-dd HH mm ss";
         SimpleDateFormat sdf = new SimpleDateFormat(format);
         d = sdf.parse(parseDate);
         catch(Exception e)
         System.out.println(e.toString());
         System.out.println("The Date was not of '"+format+" ' format");
         d = null;
         if(d==null) {
              try
                        format = "yyyy-MM-dd HH:mm:ss";
              SimpleDateFormat sdf = new SimpleDateFormat(format);
              d = sdf.parse(parseDate);
              catch(Exception e)
              System.out.println(e.toString());
              System.out.println("The Date was not of '"+format+" ' format");
              d = null;
         if(d==null) {
              try
                        format = "yyyy-MM-dd HH_mm_ss";
              SimpleDateFormat sdf = new SimpleDateFormat(format);
              d = sdf.parse(parseDate);
              catch(Exception e)
              System.out.println(e.toString());
              System.out.println("The Date was not of '"+format+" ' format");
              d = null;
         if(d==null) {
              try
                        format = "yyyy-MM-dd HH-mm-ss";
              SimpleDateFormat sdf = new SimpleDateFormat(format);
              d = sdf.parse(parseDate);
              catch(Exception e)
              System.out.println(e.toString());
              System.out.println("The Date was not of '"+format+" ' format");
              d = null;
         if(d==null) {
              try
                        format = "yyyy-MM-dd-HH-mm-ss";
              SimpleDateFormat sdf = new SimpleDateFormat(format);
              d = sdf.parse(parseDate);
              catch(Exception e)
              System.out.println(e.toString());
              System.out.println("The Date was not of '"+format+" ' format");
              d = null;
         if(d==null) {
              try
                        format = "yyyyMMddHHmmss";
              SimpleDateFormat sdf = new SimpleDateFormat(format);
              d = sdf.parse(parseDate);
              catch(Exception e)
              System.out.println(e.toString());
              System.out.println("The Date was not of '"+format+" ' format");
              d = null;
         if(d==null) {
              try
                        format = "MM-dd-yyyy HH:mm:ss";
              SimpleDateFormat sdf = new SimpleDateFormat(format);
              d = sdf.parse(parseDate);
              catch(Exception e)
              System.out.println(e.toString());
              System.out.println("The Date was not of '"+format+" ' format");
              d = null;
              if(d==null) {
              try
                        format = "dd-MM-yyyy HH:mm:ss";
              SimpleDateFormat sdf = new SimpleDateFormat(format);
              d = sdf.parse(parseDate);
              catch(Exception e)
              System.out.println(e.toString());
              System.out.println("The Date was not of '"+format+" ' format");
              d = null;
         if(d==null) {
              try
                        format = "MM:dd:yyyy HH:mm:ss";
              SimpleDateFormat sdf = new SimpleDateFormat(format);
              d = sdf.parse(parseDate);
              catch(Exception e)
              System.out.println(e.toString());
              System.out.println("The Date was not of '"+format+" ' format");
              d = null;
         if(d==null) {
              try
                        format = "dd:MM:yyyy HH:mm:ss";
              SimpleDateFormat sdf = new SimpleDateFormat(format);
              d = sdf.parse(parseDate);
              catch(Exception e)
              System.out.println(e.toString());
              System.out.println("The Date was not of '"+format+" ' format");
              d = null;
    if(d==null) {
              try
                        format = "yyyy:MM:dd HH:mm:ss";
              SimpleDateFormat sdf = new SimpleDateFormat(format);
              d = sdf.parse(parseDate);
              catch(Exception e)
              System.out.println(e.toString());
              System.out.println("The Date was not of '"+format+" ' format");
              d = null;
         if(d==null) {
              System.err.println("None of the Date format worked. Please modify the calling method and provide a specific format");
         return d;
    }

Maybe you are looking for

  • Need to know what to do with new phone and no screen and no support from Telus Store

    Purchased a brand new BB Curve 8530, and resigned another three year plan at Telus. Within 20 days, the screen went black and would not light up. I had used the phone in the morning to send a textmail, then set it on the charger. Later in the day, I

  • Help with Distinct or Grouping to get rid of duplicates

    So Im stumped at the approach I need to take. have a query here: select type_type, RCD_REASON, rc.description, MATERIAL_ID, USERID, DATETIME_SCRAPPED, ORDER_NUMBER, TRIMDATE, SUBSTR(SALES_ORDER,3,8) || '-' || SUBSTR(SOL_LINE_NUMBER,3,4) AS SalesOrder

  • Maintain Infotype Text using BDC

    Hello All, How to maintain infotype text using BDC (Edit->maintain text). Thanks, Rushi

  • IPad won't recognize Nikon D300

    I've had no luck connecting my D300 to my iPad with the camera connection kit. Nothing shows up on the iPad screen, not even an "inability to mount" box (which I get with my old D200). I've tried reformatting the memory card, switching between mass s

  • Downloading into a text file with comma seperation

    hey experts, well i want to download various fields of an internal table into a text file.but the hitch is that all the columns should be seperated by a comma.something like csv. could you please help me with this.? i have tried using gui download bu