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

Similar Messages

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

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

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

  • 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

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

  • 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());
    }

  • From string to date format

    I have 2 strings (eg: "1232" and "1332"). These 2 strings are values of an hour (12h32 and 13h32). Important is that these values are not always the same.
    Now what I have to do is to calculate some given numbers between these hours.
    Example:
    Let's take the 2 hours mentioned above: 12h32 and 13h32.
    And I have to calculate 10 numbers between these hours.
    I have to take every 6 minutes of the given hour: 12h38,12h44,...
    When I have calculate the numbers I have to give back a string "1238","1244"
    How can I put the the strings into the dateformat 'HH:MM' and visa versa?
    Or is there a better way to calculate these numbers?
    can someone help me,please?
    Thanks in advance!!!

    A small example:public class Test {
        private LTime startTime;
        private LTime endTime;
        private int count;
        public Test (String startTime, String endTime, int count) {
            this.startTime = new LTime (startTime);
            this.endTime = new LTime (endTime);
            this.count = count;
        public String[] getTimesBetween () {
            String[] timesBetween = new String[count - 1];
            LTime lTime = new LTime (startTime);
            int minutesBetweenStartAndEnd = startTime.getMinutesBetween (endTime) / count;
            for (int i = 0; i < count; i ++) {
                if (i > 0) {
                    timesBetween[i - 1] = lTime.toString ();
                lTime.add (0, minutesBetweenStartAndEnd);
            return timesBetween;
        private class LTime {
            private int hours;
            private int minutes;
            public LTime (String lTime) {
                this (Integer.parseInt (lTime.substring (0, lTime.indexOf ("h"))), Integer.parseInt (lTime.substring (lTime.indexOf ("h") + 1)));
            public LTime (LTime lTime) {
                this (lTime.hours, lTime.minutes);
            public LTime (int hours, int minutes) {
                this.hours = hours;
                this.minutes = minutes;
            public int getHours () {
                return hours;
            public int getMinutes () {
                return minutes;
            public void add (int hours, int minutes) {
                this.minutes += minutes;
                this.hours += hours + this.minutes / 60;
                this.minutes %= 60;
            public int getMinutesBetween (LTime lTime) {
                return isAfter (lTime) ? (hours - lTime.hours) * 60 + minutes - lTime.minutes : lTime.getMinutesBetween (this);
            public boolean isAfter (LTime lTime) {
                return (hours > lTime.hours) || ((hours == lTime.hours) && (minutes > lTime.minutes));
            public String toString () {
                return fill (hours) + "h" + fill (minutes);
            private String fill (int i) {
                return i < 10 ? "0" + i : String.valueOf (i);
        public static void main (String[] parameters) {
            String[] timesBetween = new Test ("8h30", "16h30", 12).getTimesBetween ();
            for (int i = 0; i < timesBetween.length; i ++) {
                System.out.print (((i == 0) ? "" : ", ") + timesBetween);
    }You might want to fix the following issues:
    * the count is not correct: if you specify 10, you get 9
    * the time class is for some reason named LTime
    * Because of the int division, you can get enormous
    differences between the end time that getTimesBetween ()
    returns and the actual end time
    Of course, I could have gotten rid of these myself, but
    it's your homework...
    Kind regards,
      Levi

  • 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);

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

  • String to DATS format conversion in XI.

    Hello Experts,
    I am using Powerbuilder system to get my Legacy data.In Powerbuilder Date needs to be supplied in STRING format.So I am giving String Format (dd-mmm-yyyy).
    However, at SAP end, Date needs to be in DATS format (Standard SAP yyyymmdd) .
    In Message Mapping I have given I/P structure as String and O/P structure as DATS format. When I test mapping in XI, it gives me Parseable error.
    Can anybody tell me, is there any way in XI to convert from String to DATS format so that I do not get Parseable error.
    Please Help!!!!

    Lokesh,
    With XI there is a date transformation function under date category there you need to provide source date format and target date format..
    I have used the same for fetching the data from oracle..and posting into SAP..it works fine..
    Try this ..this will solve ur problem....you just need to see what is the exact date format used by powerbuilder internally...
    Regards,

  • String to DATS format Conversion

    Hello Experts,
    I am using Powerbuilder system to get my Legacy data.In Powerbuilder Date needs to be supplied in STRING format.So I am giving String Format (dd-mmm-yyyy).
    However, at SAP end, Date needs to be in DATS format (Standard SAP yyyymmdd) .
    In Message Mapping I have given I/P structure as String and O/P structure as DATS format. When I test mapping in XI, it gives me Parseable error.
    Can anybody tell me, is there any way in XI to convert from String to DATS format so that I do not get Parseable error.
    Please Help!!!!

    Lokesh,
    With XI there is a date transformation function under date category there you need to provide source date format and target date format..
    I have used the same for fetching the data from oracle..and posting into SAP..it works fine..
    Try this ..this will solve ur problem....you just need to see what is the exact date format used by powerbuilder internally...
    Regards,

  • Conversion from string "20041023 " to type 'Date' is not valid.

    Hi ,
       I have a table with one of the column(EmpHiredate) datatype is char(10). It has value like "20141023". I need to display this value as date format(dd/mm/yyyy) in report. 
    Following methods i tried in textbox expression but no luck.
    =Format(Fields!EmpHireDate.Value,"dd/MM/yyyy")
    =Cdate(Fields!EmpHireDate.Value)
    Error:
    [rsRuntimeErrorInExpression] The Value expression for the textrun ‘EmpHireDate.Paragraphs[0].TextRuns[0]’ contains an error: Conversion from string "20041023  " to type 'Date' is not valid.
    Is it possible to convert string to date using SSRS textbox expression ? Can anyone help me with the solution.
    Thanks,
    Kittu

    Hi Jmcmullen,
         Found one more issue on the same. I have one value like "00000000" for the column(EmpHiredate)
    , when i use above expression values(ex:"20141023")
    are displaying in dd/MM/yyyy format in report except value like "00000000" and giving following error:
    [rsRuntimeErrorInExpression] The Value expression for the textrun ‘EmpHireDate.Paragraphs[0].TextRuns[0]’ contains an error: Conversion from string "0000/00/00" to type 'Date' is not valid.
    Even i tried to pass its original value("00000000") as below but no luck.
    =IIF(Fields!EmpHireDate.Value = "00000000","00000000",Format(CDATE(MID(Fields!EmpHireDate.Value,1,4) + "/" + MID(Fields!EmpHireDate.Value,5,2) + "/" + MID(Fields!EmpHireDate.Value,7,2)),"dd/MM/yyyy"))
    Also tried this:
    =IIF(Fields!EmpHireDate.Value = "00000000","2000/10/21",Format(CDATE(MID(Fields!EmpHireDate.Value,1,4) + "/" + MID(Fields!EmpHireDate.Value,5,2) + "/" + MID(Fields!EmpHireDate.Value,7,2)),"dd/MM/yyyy"))
    Please Suggest. 
    Thanks ,
    Kittu

  • Determine date from string

    Hi, I have an application that reads in information files in text format. Each line is made up of some character delimited fields and one of these fields is a date field. This information files come from can come from many different sources and can have different formats, ie the columns in different orders and the date in different formats. The file will always be the same from a particular source so I wrote a utility into my program that you can run which lets to create a defintion file mapping the column in the file to the appropriate field, so when you import you just choose the right definition and it reads everything correctly. The problem is with the date. It can possibly come in many different formats. Is there any way to determine the date from a string if you are unsure of the format when it is accessed? So if "2004-04-15" was brought in as a string the applicaiton would know that this is year 2004, march 15. I know you can use SimpleDateFormat to parse strings into dates but it requires you to know the format first. My question is just how can I figure out this format, or at least have a nice clean way for the user to define a custom date format when they are creating a definition for their information file. Any help is appreciated.

    from Europe, for instance, and you get "04.04.2004" - what date is that?I know that one! That's the fourth of April! ;-)I can't believe I did that.Don't try to get me started on all my little bloopers ;-)
    Friends of mine, one from the Czech Republic and one from the US, got married Aug 8th just so the
    short date would make sense to all interested parties.Fourteen years ago, people overhere (Netherlands) wanted to get married thirtyfour minutes and
    sixtyfive seconds past noon on the seventh of August in nineteenninety. It would make up for a nice
    12:34:56 7/8/90
    I meant 04.08.2004 - that's what I meant, you see.I know, sorry for the silly joke.
    kind regards,
    Jos

Maybe you are looking for