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

Similar Messages

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

  • I want convert string to date format in Oracle

    Dear All
    I want convert string to date format in Oracle,Format is given below
    'Friday, 02 March 2012 2:44 PM' to '02/03/2012 2:44 PM'

    >
    Hi Parwez,
    I want convert string to date format in Oracle,Format is given below
    'Friday, 02 March 2012 2:44 PM' to '02/03/2012 2:44 PM'SELECT TO_DATE('Friday, 02 March 2012 2:44 P.M.', 'DAY, DD MONTH YYYY HH:MI A.M.') from dual;
    As well as what the other poster suggested, look here: http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34924
    HTH,
    Paul...

  • Get Time Stamp will milli seconds from string time stamp format

    Hi Guys,
    I am trying to get the time stamp with milli seconds value from string format time data,
    Attached the snippet (Get milli seconds to Time stamp)I  tried.
    Attached Sub VI Convert string to time stamp
    When run,I am not getting the time string indicator with milli seconds value even though the format have <%2u> .
    please guide me on this.
     ..AND Finally i will be converting the date and time string output to time stamp data type
    Attachments:
    Convert_String_to_TimeStamp.vi ‏13 KB
    Get Milli seconds to Time stamp.png ‏25 KB

    You do realize you could have just used a single Scan From String to get the timestamp, right?
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines
    Attachments:
    Get Timestamp.png ‏43 KB

  • String into date format

    Hi
    I was wondering if anyone knew how to convert a string into different formats,
    to enable it to be stored in a mysql db.
    I was wondering anyone you knew how to convert:
    - String into the date format yyyy-mm--dd
    - String into time format 00:00:00
    - string to double
    Ive tried:
    c_date = java.sql.Date.valueOf(f_date);
    System.out.println("conversion data: " +c_date);
    c_time = java.sql.Time.valueOf(f_time);
    System.out.println(" time converted data: "+c_time);
    // string to double conversion
    c_change = java.lang.Double.valueOf(f_change);
    System.out.println(c_change);
    My class doesnt execute this at all?
    Also,
    SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM-dd");
    c_date = sdft.format(f_date);
    System.out.println(c_date);
    .. but this method returns the error incompatible types?
    Any suggestions would be helpful.. thanks in advance..

    Friends i've got similar kind of problem...can you help me
    here is my code. When i try to print the user entered date (dd/MM/yyyy)(which i am storing in a string) the program dispalys nothing. and everey time i enter a valid date it displas "invalid From date entered ". I need to store the user entered date into a string because i need that for further use. All my intesion is to get two dates from user in dd/MM/yyyy. Strore them in certain variable. Check if they are valid or not. and make sure todate is either equal or greater than fromdate. Please help me to solve this problem.
    public class EDTDateValidation extends JFrame implements ActionListener{
    private JLabel fromlabel;
    private JLabel tolabel;
    private JTextField fromtxt;
    private JTextField totxt;
    private String fmt ="dd/MM/yyyy";
    private java.lang.String fromdate;
    private java.lang.String todate;
    private JButton buttonOK;
    private JButton buttonCancel;
    private Date theDate;
    private Date date1;
    private Date date2;
    private JPanel mainPanel;
    SimpleDateFormat dtformat = new SimpleDateFormat(fmt);
    public EDTDateValidation(){
    super("Date Validation");
    dtformat.setLenient(false);
    mainPanel=new JPanel();
    mainPanel.setLayout(null);
    fromlabel = new JLabel("From Date");
    tolabel = new JLabel("To Date");
    buttonOK = new JButton("OK");
    buttonCancel = new JButton("Cancel");
    fromdate = new String();
    todate = new String();
    fromtxt = new JTextField(10);
    totxt = new JTextField(10);
    fromdate = fromtxt.getText();
    todate = totxt.getText();
    mainPanel.add(fromlabel);
    fromlabel.setBounds(20,20,50,15);
    mainPanel.add(tolabel);
    tolabel.setBounds(20,50,50,15);
    mainPanel.add(fromtxt);
    fromtxt.setBounds(90,20,130,20);
    mainPanel.add(totxt);
    totxt. setBounds(90,50,130,20);
    mainPanel.add(buttonOK);
    buttonOK.setBounds(70,80,71,23);
    mainPanel.add(buttonCancel);
    buttonCancel.setBounds(150,80,71,23);
    buttonOK.addActionListener(this);
    buttonCancel.addActionListener(this);
    setContentPane(mainPanel);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(247,140);
    setResizable(false);
    //pack();
    public static void main(String args[]) {
    try {
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    catch(Exception e) {
    System.err.println("Could not load Look and Feel" + e);
    EDTDateValidation edtDateVal = new EDTDateValidation();
    edtDateVal.setVisible(true);
    public void actionPerformed(ActionEvent e) {
    if(e.getSource() == buttonOK){
    System.out.println(fromtxt.getText());
    System.out.println(fromdate); //THIS PRINTS BLANK
    System.out.println(todate); //THIS ASWELL
    try {
    Date date = null;
    date = dtformat.parse(fromdate);
    System.out.println("valid From date entered!");
    catch(Exception f) {
    System.out.println("Invalid From date entered!");
    // textField1.setText("");
    return;
    try {
    Date date = null;
    date = dtformat.parse(todate);
    System.out.println("valid TO date entered!");
    catch(Exception f) {
    System.out.println("Invalid To date entered!");
    // textField1.setText("");
    return;
    }

  • Test String for date format

    Is there a function that reads a string and can return yes/no if the string matches a specified date format (example: mm/dd/yy)? I wish to avoid parsing the string character by character.
    Thanks,
    George

    BTW,
    I used the following example code available from this site. I was able to easily modify it to sovle my problem.
    try {
    SimpleDateFormat formatter =
    new SimpleDateFormat("EEEE, MMMM dd, yyyy");
    Date d = formatter.parse(
    "Tuesday, January 03, 1956");
    formatter = new SimpleDateFormat(
    "EE, MMM d, yy");
    d = formatter.parse("Tue, Jan 3, 56");
    formatter = new SimpleDateFormat(
    "EE, MM d, yy");
    d = formatter.parse("Tue, 01 3, 56");
    process(d);
    } catch (ParseException e) {
    On to the next challenge...
    George

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

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

  • 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

  • How to convert string to date format?

    Hi All,
    String is in following format: 2006-12-07 i.e yyyy-mm-dd
    I want it in 07.12.2006 date format.
    which function module should i used to convert it?
    Regards,
    Nilima

    an other way to do this :
    Code
    DATA :
    ld_text(20) TYPE c,
    ld_date TYPE datum.
    ld_text = '2006-12-07'.
    REPLACE ALL OCCURENCES OF '-' IN ld_text WITH ''.
    WRITE ld_text TO ld_date.
    WRITE ld_date.

  • Formating string to date format in vc

    hai,
    function module in production return a date in type of char10. now i need to convert into proper data format.
    can any one help me.

    data: l_date type datum
          , l_d(10)
    l_date = sy-datum.
    write l_date to l_d. -> This statement will convert into user date format
    write:/ l_d.  
    ...Naddy

  • From varchar to date format mm/dd/yyyy

    Hi everyone,
    I am pretty sure this question has been asked before, but I've been going through the blog for quite a while and can't seem to find anything!
    Here is my problem. I am trying to compare a field in my database with a certain date, but the field is a varchar in the format mddyyyy, without comas or bars or anything, and I need to campare it with a date of the format mm/dd/yyy.
    In Excel I used a quite complicated formula:
    Right('0000000000' & [Maturity_Date],8) AS [New Mat #],
    CDate(Left([New Mat #],2) & "/" & Mid([New Mat #],3,2) & "/" & Right([New Mat #],4)) AS [Mat Date],
    But, to start with, Oracle doesn't seem to have the "right" operator.
    Anyone could give me an idea.
    Thanks for your time!! :)

    Hi,
    849073 wrote:
    Hi!
    Thank you very much, for your fast answer and your ideas!
    Let me do this right then :) I'm using Oracle 9.2.0.8. Then you can't use REGEXP_REPLACE. Regular expressions were introduced in Oracle 10.
    I didn't write the query beacuse it is mainly a bunch of joins I have already tested and they are right. I got the problem when inserting the date.
    So, one of the problems is that the varchar might have only 1 digit for the month, so it would be like mddyyyy.
    LPAD ( string_a
         , 8
         , '0'
         )will return a copy of string_a, with an extra '0' added at the beginning if it's only 7 characters long. That is
    LPAD ('3312011', 8, '0')returns '03312011', which you can then use in any of the expressions I gave earlier, or in TO_DATE.
    If string_a is already 8 characters long, e.g. '12312010', then the LPAD expression above returns it unchanged.
    This is why the TODATE function will give back an error.
    Then, as you correctly stated, I need to convert the string into a DATE type, since I need to check dates before a certain other or not. I am not sure if I made myself clear :SSorry, no. That's why you need to post some sample data, and the results you want from that data. In this case, the sample data would be just 2 columns (the string and the DATE), perhaps 6 rows and the results would be the rows where the one that is supposed to be earlier really is.

Maybe you are looking for

  • Premiere CS6 Still Has a Long Way To Go

    I'm in commercial editing and work as offline editor, assistant editor and compositor.  I've been using Premiere Pro ever since FCPX came out, but not in the workplace as an offline tool because of certain major missing features: - a good EDL exporte

  • USB port malfunction - iTunes not syncing with iPod

    to all those apple veterans, I am using my boyfriend's ipod and in the past, when i plug his ipod into my usb port on my sony computer windows XP, it has always appeared under 'my computers" where i could register my files. I had my itunes library fo

  • Swaping Panels in a frame?

    Hey, I have a frame that has a GridLayout of (1,2) so it can jold 2 Panels of equal size, when I press a button i need the 2 Panels in there to go away so I can put new ones in. This is my code to add Panels: public void addPanels()          frame.ge

  • Safari's search bar overlaps website

    The top of Safari (where the search bar, all the tabs and so on is) is overlapping the webpage I'm on. This sometimes goes over the website's header. Here is a picture of the problem on Apple's website: This is particularly annoying for websites with

  • Getting Composer on Purchased Songs

    I am not getting the name of the composer on any of the songs I buy through iTunes. Is there something I need to do to have them show up or is this an iTunes thing? hp pavilion zf1000   Windows XP