1 2 Previous Next 23 Replies Latest reply: Jun 3, 2013 11:01 AM by aksarben RSS

    Date format

    1012252
      Hi
      I would like to ask how can I convert date format to a different formats in Java. I am trying to read the 12h format date from txt file and save the date in csv but in new 24h format. Could somebody tell me how I can do this. I tried to use SimpleDateFormat but it is not working for me as I am getting exception that Object can't be converted to Date.

      public void writeDate(BufferedWriter writer) throws IOException {
                writer.write(writeDate(datetime) + columDelimiter);
                writer.write(writeDate(id) + columnDelimiter);
                writer.newLine();


      Thanks
        • 1. Re: Date format
          TPD-Opitz
          Welcome to the forum.

          Have a the API, it holds a lot of useful information:
          http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

          bye
          TPD
          • 2. Re: Date format
            rp0428
            >
            I would like to ask how can I convert date format to a different formats in Java. I am trying to read the 12h format date from txt file and save the date in csv but in new 24h format. Could somebody tell me how I can do this. I tried to use SimpleDateFormat but it is not working for me as I am getting exception that Object can't be converted to Date.

            public void writeDate(BufferedWriter writer) throws IOException {
            writer.write(writeDate(datetime) + columDelimiter);
            writer.write(writeDate(id) + columnDelimiter);
            writer.newLine();
            >
            No one can see what is wrong with your code if you don't post your code.
            • 3. Re: Date format
              1012252
              Thanks for the replies. I tried to use the SimpleDateFormat but it's not working for me

              public void writeDate(BufferedWriter writer) throws IOException {
              SimpleDateFormat format = new SimpleDateFormat("YYY-MM-DD hh:mm:ss");
              String formatted = format.format(datetime);
              writer.write(writeDate(formatted) + columDelimiter);
              writer.write(writeDate(id) + columnDelimiter);
              writer.newLine();
              }
              the datetime is a date readed from file is is not today date

              I also tried to use substring but without success

              public void writeDate(BufferedWriter writer) throws IOException {
              String hour = datetime.substring(13,24);
                        int hourInt = Integer.parseInt(hour);
                        if(hour.substring(21,24).equals("PM"))
                             hourInt += 12;
                             else if(hour.substring(13,15).equals("12") && hour.substring(21,24).equals("AM"))
                             hourInt = 0;
                        writer.write(writeCSV(datetime.substring(8,12)+"-"+datetime.substring(0,3)+"-"+datetime.substring(4,6)) + hour.substring(13,24) + columnDelimiter);
              }

              Thanks again
              • 4. Re: Date format
                TPD-Opitz
                Did you find the <tt>parse()</tt>-Method in SimpleDateFormat?

                bye
                TT
                • 5. Re: Date format
                  1012252
                  Hi

                  Can't use parse as it is giving me an error, the line there will be underlayed as red. Do you have any other suggestion?

                  Thanks
                  • 6. Re: Date format
                    TPD-Opitz
                    And what is the actual error the compiler shows?

                    bye
                    TPD
                    • 7. Re: Date format
                      1012252
                      Hi

                      The error is showing cannot convert from Date to String so that why I tried to use format instead of parse.

                      Thanks
                      • 8. Re: Date format
                        TPD-Opitz
                        Are you aware that you have 2 processes here?
                        1. convert the String you read from the file to Java internal Date type.
                        2. format this date to the form you want to display.

                        Knowing this you need 2 instances of <tt>SimpleDateFormat</tt>. One representing the files DateFormat used for parsing it and the second representimng your target format to use <tt>format()</tt>-Methos on.

                        bye
                        TPD
                        • 9. Re: Date format
                          1012252
                          Hi,

                          Could you tell me if I am using it right as I still cannot parse, the datetime is readed from file so how use a second instance to format it

                          Date d = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").parse(datetime);
                          String s2 = (new SimpleDateFormat("MM/dd/yyyy hh:mm a")).format(d);

                          Thanks

                          Edited by: 1009249 on Jun 1, 2013 9:39 AM

                          Edited by: 1009249 on Jun 1, 2013 9:39 AM
                          • 10. Re: Date format
                            TPD-Opitz
                            this looks ok.

                            bye
                            TPD
                            • 11. Re: Date format
                              1012252
                              Hi,

                              As using the previous code I still can't parse I tried ue this

                              DateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSS000");
                                        String theDate = format.format(datetime);
                                        // this will fail
                                        try {
                                             format.parse(theDate);
                                        } catch (java.text.ParseException e) {
                                             // TODO Auto-generated catch block
                                             e.printStackTrace();
                                        }

                              But compiler gives me stacktrace exception. Could you provide me an example how to correctly use parse and format if the datetime is read from file and it is not today date.

                              Thanks again
                              • 12. Re: Date format
                                TPD-Opitz
                                remove the trailing zeros: <tt>new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSS");</tt>

                                bye
                                TPD
                                • 13. Re: Date format
                                  1012252
                                  Hi

                                  It is still not solving my problem as I am still getting exception but anyway thank for you time
                                  • 14. Re: Date format
                                    1012252
                                    Hi

                                    It is still not solving my problem as I am still getting exception but anyway thank for you time
                                    1 2 Previous Next