Java Beginners

Moderators: zibadian
Number of threads: 1285
Number of posts: 2739

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
creating a Time class using System.currentTimeMillis() Posted by Bravid2001 on 18 Oct 2011 at 12:04 PM
Hello,

1st post on this forum...

I am making a class called Time where I use System.currentTimeMillis() to display the current time. Then I create another constructor to get the time using 555550000 elapsed milliseconds. I have created both a Time class and a UseTime class. When I use the UseTime class, it gives me 0 hours, 0 minutes, and counts the seconds. I am trying to debug. My 1st debug is to test the Time class to see if it actually gives the correct time. But how do I use a println statement in the Time class to test it out?

Below is my code. Any help here is appreciated.

public class Time{

	//Obtain the total milliseconds since midnight, Jan 1, 1970
	private long totalMilliseconds;
	private long totalSeconds;
	private long currentSecond;
	private long totalMinutes;
	private long currentMinute;
	private long totalHours;
	private long currentHour;

	/*
	Default constructor
	/
	*/

	public Time()
	{
		totalMilliseconds = System.currentTimeMillis();
		totalSeconds = totalMilliseconds / 1000;
		currentSecond = (int)(totalSeconds % 60);
		totalMinutes = currentSecond / 60;
		currentMinute = totalMinutes % 60;
		totalHours = totalMinutes / 60;
		currentHour = totalHours % 24;
	}

	public Time(long newTotalMilliseconds, long newTotalSeconds, long newCurrentSecond, long newTotalMinutes, long newCurrentMinute,
		long newTotalHours, long newCurrentHour)
	{
		totalMilliseconds = newTotalMilliseconds;
		totalSeconds = newTotalSeconds;
		currentSecond = newCurrentSecond;
		totalMinutes = newTotalMinutes;
		currentMinute = newCurrentMinute;
		totalHours = newTotalHours;
		currentHour = newCurrentHour;
	}

	//getter
	public long getTotalMilliseconds ()
	{
		return totalMilliseconds;
	}

	//setter
	public void setTotalMilliseconds(long newTotalMilliseconds)
	{
		totalMilliseconds = newTotalMilliseconds;
	}

	//getter
	public long getTotalSeconds ()
	{
		return totalSeconds;
	}

	//setter
	public void setTotalSeconds(long newTotalSeconds)
	{
		totalSeconds = newTotalSeconds;
	}

	//getter
	public long getCurrentSecond()
	{
		return currentSecond;
	}

	//setter
	public void setCurrentSecond(long newCurrentSecond)
	{
		currentSecond = newCurrentSecond;
	}

	//getter
	public long getTotalMinutes()
	{
		return totalMinutes;
	}

	//setter
	public void setTotalMinutes(long newTotalMinutes)
	{
		totalMinutes = newTotalMinutes;
	}

	//getter
	public long getCurrentMinute()
	{
		return currentMinute;
	}

	//setter
	public void setCurrentMinute(long newCurrentMinute)
	{
		currentMinute = newCurrentMinute;
	}

	//getter
	public long getTotalHours()
	{
		return totalHours;
	}

	//setter
	public void setTotalHours(long newTotalHours)
	{
		totalHours = newTotalHours;
	}

	//getter
	public long getCurrentHour()
	{
		return currentHour;
	}

	//setter
	public void setCurrentHour(long newCurrentHour)
	{
		currentHour = newCurrentHour;
	}

	//toString method
		public String toString()
		{
			return "The current time is " + getCurrentHour() + ":" + getCurrentMinute() +  ":" +  getCurrentSecond();
		}

}

Attachment: Time.java (2572 Bytes | downloaded 39 times)



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.