Representing time field in java class

Hi All,

how do i represent this time field in my java class

I would like the format when outputted to be
recording time 4:38.284
completed time 2:27:39.484
rosy2015Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeffrey Dake Senior Director of TechnologyCommented:
when outputting your date object you can use a date formater to output that time in whatever format you want.

For the output you want you can use a http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Hope this is what you are looking for
rosy2015Author Commented:
further help needed, here's my class and my main class, i would like to format both fields before outputting, new to java, how do i do this?


import java.util.Calendar;

public class Recording {
   private long  recordingTime = Calendar.getInstance().getTimeInMillis();
   private long  completedTime = Calendar.getInstance().getTimeInMillis();
         
    public Recording( long tRecordingTime, long tCompletedTime)
    {
        this.recordingTime =tRecordingTime;
        this.completedTime = tCompletedTime;
    }

    public long getRecordingTime()
   {
        return recordingTime;
       
   }
 
   public void setRecordingTime(long value)
   {
       recordingTime = value;
       
   }
   
    public long getCompletedTime()
   {
        return completedTime;    
   }
 
   public void setCompletedTime(long value)
   {
       completedTime = value;
       
   }
   
    @Override
     public String toString()
            //print information
    {
        return "("+ "\t" + recordingTime +  "\t" + completedTime +  "\t " 
                +"\n";
    }
}


//main class


import java.util.ArrayList;
import java.util.List;

public class Album {
    public static void main(String[] args) {
        // TODO code application logic here
        // TODO code application logic here
      List<Recording> record = new ArrayList<>();
       Recording record1 = new Recording(137481,672745103);
       record.add(record1);
       Recording record2 = new Recording(937176,68652108);
       record.add(record2);
       Recording record3 = new Recording(198459,69738107);
       record.add(record3);
       Recording record4 = new Recording(238264,69722104);
       record.add(record4);
     
       System.out.println(record.toString());
       
    }
   
}
Jeffrey Dake Senior Director of TechnologyCommented:
So since you are using milliseconds to keep track of the time, you can convert that back to a date object.  You can then format that date object.

Date recordingDate = new Date(recordingTime);
Date completedTimeDate = new Date(completedTime);
DateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS");
String dateFormatted = formatter.format(recordingDate);
String completedFormatDate = formater.format(completedTimeDate);

You can then use those Strings to print out the results in you toString() method.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

rosy2015Author Commented:
Hi tried this in the class and it did not work

public void setRecordingTime(long value)
   {
       Date recordingDate = new Date(recordingTime);
        Date completedTimeDate = new Date(completedTime);
        DateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS");
        String dateFormatted = formatter.format(recordingDate);
        String completedFormatDate = formatter.format(completedTimeDate);

       recordingTime = value;
       
   }
Jeffrey Dake Senior Director of TechnologyCommented:
Sorry, I meant that you could change your toString method to the following:

   @Override
   public String toString()
          //print information
  {
      Date recordingDate = new Date(recordingTime);
      Date completedTimeDate = new Date(completedTime);
      DateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS");
      String dateFormatted = formatter.format(recordingDate);
      String completedFormatDate = formatter.format(completedTimeDate);
     
      return "("+ "\t" + dateFormatted +  "\t" + completedFormatDate +  "\t "
              +"\n";
  }

Now the dates will be formatted in your toString() method, which is what is being called when you call.
System.out.println(record.toString()); on each on of your Recording  objects.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rosy2015Author Commented:
Thanks Jeffery! Please look out for my other questions, i'm self teaching myself Java :-)
rosy2015Author Commented:
Ace answer and quick feedback, you rock!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.