Parsing from string to date object

How can I convert a time in String format to a Date object?
ie. 14:30:05(String) to 14:30:05

How about this?
String sDate = "14:30:05";
StringTokenizer tokenizer = new StringTokenizer(sDate, ":");
Calendar calendar = new Calendar();
int hour = Integer.parseInt(tokenizer.nextToken());
int minute = Integer.parseInt(tokenizer.nextToken());
int second = Integer.parseInt(tokenizer.nextToken());
calendar.set(Calendar.HOUR_OF_DAY, int hour);
calendar.set(Calendar.MINUTE, int minute);
calendar.set(Calendar.SECOND, int second);
Date dDate = calendar.getTime();I didn't try it, but it should be pretty close (unless you want to use deprecated methods, then it can be shorter...)

Similar Messages

  • Date string to Date object

    Hi
    How would i go about parsing some date strings in DD/MM/YYYY
    format into a date objects. I am sure its an easy problem but i
    cant find a simple way to do it as most of the API's want it a
    differnt format like MM/DD/YYYY
    Thanks for any help

    Hi,
    in the actionscript 3.0 cookbook there is a class DateFormat
    described, which allows to parse strings to date objects.
    You can download the code of the dateformat.as from here .
    http://www.rightactionscript.com/ascb/
    best regards,
    kcell

  • 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.

  • Date contructor deprecation : Parsing a String to Date

    Hi All,
    In Java 1.5 the Date() constructor Date(String s) is deprecated. As per the API Documentation DateFormat.Parse() method is used.
    The following code from Java 1.4 version has to be upgraded to Java 1.5.
    Existing Code:
    Date dDate = new Date(sDate);
    Modified Code:
    DateFormat df = DateFormat.getDateInstance();
    Date dDate = df.parse(sDate);
    Here the DateFormat accepts a default formatting style as "Feb 01, 2007" and parses the String.
    If the String sDate belongs to any other formatting style such as "01 Feb, 2007" or "01 Feb, 07" the code piece throws unparsable date error.
    Please give your thougts on this issue to parse the string of any format..
    Thanks,
    Rajesh.

    Hi All,
    In Java 1.5 the Date() constructor Date(String s) is
    deprecated. As per the API Documentation
    DateFormat.Parse() method is used.
    The following code from Java 1.4 version has to be
    upgraded to Java 1.5.
    Existing Code:
    Date dDate = new Date(sDate);
    Modified Code:
    DateFormat df = DateFormat.getDateInstance();
    Date dDate = df.parse(sDate);
    Here the DateFormat accepts a default formatting
    style as "Feb 01, 2007" and parses the String.
    If the String sDate belongs to any other formatting
    style such as "01 Feb, 2007" or "01 Feb, 07" the code
    piece throws unparsable date error.
    Please give your thougts on this issue to parse the
    string of any format..You can't. What date is this: "08/04/24"? 8 April, 1924? 4 August, 2024?
    >
    Thanks,
    Rajesh.

  • 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

  • Hoe to pass String to Date object

    Guys,
    I want to convert the String 01/03/07 09:15:26 to a Date object.
    Pls guide me.

    Hi,
    Try the following:
                    DateFormat df = new SimpleDateFormat ("dd/MM/yy hh:mm:ss");
              try
                   Date date = df.parse("01/03/07 09:15:26");
                   System.out.println(date);
              catch (ParseException e)
                   e.printStackTrace();
              }Regards,
    Kumar

  • 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!

  • Facing Problem in parsing a string to date

    Hi,
    I was trying to change a string into date with date format ("EEEE,MMM,d,h:mm") but I always get the year as 1970.
    here is my code
    String strDate="Saturday,Jan 19 7:31";
    String dateFormat3="EEEE,MMM,d,h:mm";
         try {
         DateFormat myDateFormat = new SimpleDateFormat(dateFormat3);
         result1=myDateFormat.parse(strDate);
    catch(ParseException pe) {
                System.out.println("ERROR: could not parse date in string \"" +
            }any solution for it.

    This is my actual code
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Locale;
    public class TestingDate {
          * @param args
         public static void main(String[] args) {
              // TODO Auto-generated method stub
              String dateFormat="EEEE, MMM d h:mm a";
              Date test=new Date(2007,0,19, 19, 31);
              System.out.println(" original date is "+test);
              String stringResult=DateToString(test,dateFormat);
              System.out.println("Date to string is "+stringResult);
              Date dateResult=stringToDate(stringResult,dateFormat);
              System.out.println(" String to date is "+dateResult);
              String stringResult2=DateToString(dateResult,dateFormat);
              System.out.println(" Date to string  is "+stringResult2);
    public static String DateToString(Date test, String dateFormat) {
             String result = null;
             try {
                  DateFormat myDateFormat = new SimpleDateFormat(dateFormat);
                     result = myDateFormat.format(test);
                     //System.out.println(" reslut date is "+result);
              } catch (Exception e) {
                   System.out.println(" Exception is "+e);
              return result;
    public static Date stringToDate(String strDate,String dateFormat1){
         Date result1=null;
         try {
              DateFormat myDateFormat = new SimpleDateFormat(dateFormat1);
              result1=myDateFormat.parse(strDate);
         catch(Exception e){
              System.out.println(" exception is "+e);
         return result1;
    }I am facing problem in getting the actual date. Please suggest the solution.

  • How to get the currrent month and year from a new date object

    If I create a new Date object as "d",
    java.util.Date d = new java.util.Date();how can I format the date to get the current Month as 'Jan' and the current year as '2008'. So if I have something like d.getMonth() gets the current month as 'Oct' and d.getYear() gets '2008'
    Thanks,
    Zub

    [Read the flamin' manual you must. Hmm.|http://en.wikipedia.org/wiki/RTFM]
    ~~ Yoda.
    Well no actually, he didn't say that, but he should have.
    Cheers. Keith.
    PS: Don't say that to a 7 foot pissedOff wookie when he's got his head stuck in a smoking hyperdrive, and you're being chased by a S-class battle cruiser... Ask Yoda how he got to be so short.
    PPS: It is the SimpleDateFormat you seek ;-)
    Edited by: corlettk on 14/10/2008 22:37 ~~ Also far to slow... but funny.

  • From string to date

    thsi is the string format of date 10-jan-2005 but i want it in the format of java.sql.Date date format 10-jan-2005
    but its giving parse exception when i am doing that
    try {
                   dtTmp = new SimpleDateFormat("MM/dd/yy").parse(strTmp);
                   System.out.println(dtTmp);
              } catch (ParseException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
              String strOutDt = new SimpleDateFormat("dd-MMM-yyyy").format(dtTmp);
              System.out.println(strOutDt);
    can you please help me on that as after getting the output in this format i am once again try to convert strOutDt in date format ,but its giving parse exception and i want the date format as 10-jan-2005

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    public class datefr {
         * @param args
         public static void main(String[] args) {
              // TODO Auto-generated method stub
              String strTmp = "09/21/04";
              Date dtTmp = null;
              try {
                   dtTmp = new SimpleDateFormat("MM/dd/yy").parse(strTmp);
                   System.out.println(dtTmp);
              } catch (ParseException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
              String strOutDt = new SimpleDateFormat("dd-MMM-yyyy").format(dtTmp);
              System.out.println(strOutDt);
              Date date =null;
              try {
                   date = new SimpleDateFormat("MM/dd/yy").parse(strOutDt);
                        System.out.println(date);
                   } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
    the ouput is as follows
    Tue Sep 21 00:00:00 IST 2004
    21-Sep-2004
    java.text.ParseException: Unparseable date: "21-Sep-2004"
         at java.text.DateFormat.parse(DateFormat.java:335)
         at datefr.main(datefr.java:28)
    but as i told you i want the ouput 21-Sep-2004 as it is but now in java.sql.date

  • Parsing from string to int

    in my C# project I have an array type of String. the array gets numbers from type int.
    int numCell;
    string[] arr = new string[numCell];
    then i use it in a function like this:
    sum += int.Parse(arr[i]);
    and it works!
    but, when i dothe same thing in WPF it tells me:
    Input string was not in a correct format.
    what shouuld i do?

    Why did you mark your own question as answer? Please unmark it.
    >>if in C# it worked why wouldn't it work in wpf?
    As I have already mentioned, it is because of the actual value of arr[i]. It has nothing to do with Windows Forms or WPF but it is possible that your WPF application has put a different value in the array than your Windows Forms Application did. And if this
    specific value is not a valid number the parsing will always fail. So you should check the values that put into the array and probably use the int.TryParse method to do the parsing:
    int sum;
    if (!int.TryParse(arr[i], out sum))
    //the value of arr[i] was NOT a valid number...
    This will however not change the fact that for example the value "D1" cannot be converted to an int using the int.Parse or int.TryParse method.
    Please remember to close your threads by marking helpful posts as answer and please start a new thread if you have a new question.

  • Re:Parameter field change from "STRING" to "DATE"

    Hi All,
    I am calling an BEx Query to Crystal frontend.
    The BEx Query has an Variable Input ( Date Restriction ).
    The Varaible data type is DATS.
    But when I am calling the BEx Query to the Crystal front-end this varaible data type is taking as "STRING".
    I could not change the type to date at font-end as it is in display mode.We have refreshed the query but no result.
    Is there any way we can change the datatype at front-end ?
    Thanks,
    Surya Pydikondala

    Please re-post if this is still an issue to the Business Objects Integration Kits - SAP
    Forum or purchase a case and have a dedicated support engineer work with your directly

  • Conversion from string to date

    hi,
    Can someone tell me how can i convert a string "09/09/02" to date ......

    HaHaHa jboeing is Sooo instructional. Yes, please to read instructions, bang head for four hours and then do this -import java.util.*;
    import java.text.*;
    public class DateTest
       public static void main(String[] args)
          String dateStr = "09/09/02";
          Date date = new Date();
          SimpleDateFormat sDF = new SimpleDateFormat("MM/dd/yy");
          try{
             date = sDF.parse(dateStr, new ParsePosition(0));
          }catch(NullPointerException npe)
          {System.out.println("bad parse");}
          java.sql.Date sqlDate = new java.sql.Date(date.getTime());
          System.out.println(date.toString());
          System.out.println(sqlDate.toString());
    }

  • Conversion from string to Date Query.

    Hi,
    I need a way to convert a date format which is in string to a date data type.
    String is in the following format : Tue Feb 27 16:00:31 PST 2007
    Have used the SimpleDateFormat class but it returns an unparseble data error.
    Any thoughts????
    Regards
    Sam W

    Please don't crosspost: http://forum.java.sun.com/thread.jspa?threadID=5142482

  • Converstion from String to Date

    Hi,
    I need a way to convert a date format which is in string to a date data type.
    String is in the following format : Tue Feb 27 16:00:31 PST 2007
    Have used the SimpleDateFormat class but it returns an unparseble data error.
    Any thoughts????
    Regards
    Sam W

    It's my first and second thought.
    The original poster never showed us the code that didn't work for him.
    It would have taken a whole two lines. When will people learn that
    the best way to get help is to post a short example program demonstrating
    their problem? I mean, it's not rocket science. Isn't it obvious that, instead
    of writing "my code doesn't work, I get this error and oh by the way, I'm
    not actually going to show you the format string that doesn't work" they could
    just post the durn code. But no, that would make too much sense.

Maybe you are looking for