Results 1 to 6 of 6

Thread: Data format in java

  1. #1
    Join Date
    Dec 2009
    Posts
    211

    Data format in java

    Hello,
    I must convert a number of seconds in days, hours, minutes, seconds. I have a problem in the data format in java.
    Here is my code
    Code:
    Public class Clock {
     
    	Public static void hand(String[] args) {
    	   int ul = 100;
    String seconds;
    
    	   try
    	   {
    	      System.out.System.out.println( "Enter a time in seconds to convert" );
    read = System.in.read();
    	   }
    	   catch ( Exception e )
    	   {
    	      e.printStackTrace();
    	   }
           / / SimpleDateFormat sdf (read) by int seconds in days hh mm ss
    	   / / SDF format for the seconds
           SimpleDateFormat fmat = new SimpleDateFormat ("HH: mm: ss");
           String fmat = formatteddate.format(read);
    System.out.System.out.println( "it gives" + Formatteddate );
    	}
    }
    This is not working, how can I perform the task? Any help will be great for me?
    Last edited by Logan 2; 05-02-2010 at 02:13 AM.

  2. #2
    Join Date
    Nov 2009
    Posts
    335

    Re: Data format in java

    Hello,
    You can use the object Calendar
    Code:
    Calendar dt = Calendar.getInstance(); / / gets you the time of day
    / / in case you want to keep the year and dt
    
    dt.set(Calendar.HOUR, 0);
    dt.set(Calendar.MINUTE, 0);
    dt.set(Calendar.SECOND, 0);
    dt.set(Calendar.Millisecond, 0);
    
    dt.set(Calendar.SECOND, Seconds);
    Using something like this you have to get close to a solution

  3. #3
    Join Date
    Nov 2009
    Posts
    356

    Re: Data format in java

    Hi,
    Already your method of reading is incorrect, System.in.read () return code of a character and not a number (See the class with a scanner for reading advanced). Exception handling is not great, it works not but continue anyway. Finally a date is not a time and you find yourself with odd values because of the special dates (timezone, etc..). Whether you use an API that has a management durations or you implement this without using Date / Calendar & Others.

  4. #4
    Join Date
    Nov 2009
    Posts
    446

    Re: Data format in java

    Hi,
    I strongly advise you to use Joda-Time API as mentioned above, if you do not want too much to take the lead. It is well documented and very easy to use. If you need the documentation on it you can visit their official site, and also it will make your code easy to read, as it is very easy to implement. First do the above and if you have any more queries then you can ask.

  5. #5
    Join Date
    Dec 2009
    Posts
    211

    Re: Data format in java

    Hello
    I strongly advise you to use Joda-Time API as mentioned above, if you do not want too much to take the lead.
    OK I'll try this API. Actually I have never heard about this before and I do not know how to use it , if you can explain me briefly then it would be great. Or is there any way so that I can learn it easy that is I mean to say so documentation covering only the main points in it. By the way thanks for you replies and the help .

  6. #6
    Join Date
    Nov 2009
    Posts
    333

    Re: Data format in java

    Hello,
    Even I am new to java, but i have a code similar to your needs, here is the code , see if it helps you.
    Code:
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class MainClass {
      public static void main(String[] args) {
        String pat = "MM/dd/yyyy";
        SimpleDateFormat fmt = new SimpleDateFormat(pat);
        try {
          Date dt = fmt.parse("12/31/2006");
          System.out.println(dt);
        } catch (ParseException e) {
          e.printStackTrace();
        }
        // fmtting
        System.out.println(fmt.fmt(new Date()));
      }
    }

Similar Threads

  1. How to get a Java Program output in Tabular format
    By Falgu in forum Software Development
    Replies: 3
    Last Post: 18-07-2012, 04:15 PM
  2. Comparing date format with java.sql.Dat
    By KANAN14 in forum Software Development
    Replies: 5
    Last Post: 02-08-2010, 10:30 AM
  3. Using StarOffice Java API to read Excel format
    By Sabhajit in forum Software Development
    Replies: 3
    Last Post: 16-07-2010, 02:55 AM
  4. How to format a number in java?
    By kamina23 in forum Software Development
    Replies: 4
    Last Post: 20-01-2010, 09:01 PM
  5. Data recovery after format?
    By manishdk in forum Windows Software
    Replies: 7
    Last Post: 22-01-2009, 06:35 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,285,783.92508 seconds with 17 queries