I wrote a time conversion program (i.e. seconds, minutes and minutes, etc.), but later I found that these classes perform a similar operation. Is there a way to link these classes, if so, please give some solution and suggestion. Here is my code ....
Second.java
import java.util.concurrent.*; public class Second { private long secondsValue; public Second() { secondsValue = 0L; } public Second(String from, String to, long unitValue) { unitSelection(from, to, unitValue); } private void convertSecondToMinute(long unitValue) { unitValue = TimeUnit.MINUTES.convert(unitValue, TimeUnit.SECONDS); secondsValue = unitValue; } private void convertSecondToHour(long unitValue) { unitValue = TimeUnit.HOURS.convert(unitValue, TimeUnit.SECONDS); secondsValue = unitValue; } private void convertSecondToDay(long unitValue) { unitValue = TimeUnit.DAYS.convert(unitValue, TimeUnit.SECONDS); secondsValue = unitValue; } private void convertSecondToWeek(long unitValue) { unitValue = unitValue/60/60/24/7; secondsValue = unitValue; } public long getSeconds() { return secondsValue; } private void unitSelection(String from, String to, long unitValue) { if( from.equalsIgnoreCase("second")) { if(to.equalsIgnoreCase("minute")) { convertSecondToMinute(unitValue); } else if(to.equalsIgnoreCase("hour")) { convertSecondToHour(unitValue); } else if(to.equalsIgnoreCase("day")) { convertSecondToDay(unitValue); } else if(to.equalsIgnoreCase("week") ) { convertSecondToWeek(unitValue); } else { System.out.println("Invalid argument...!"); } } } }
Minute.java
import java.util.concurrent.TimeUnit; public class Minute { private long unitMinute; public Minute() { unitMinute = 0L; } public Minute(String from, String to, long unitValue) { unitSelection(from, to, unitValue); } private void convertMinuteToSecond(long unitValue) { unitValue = TimeUnit.SECONDS.convert(unitValue, TimeUnit.MINUTES); unitMinute = unitValue; } private void convertMinuteToHour(long unitValue) { unitValue = TimeUnit.HOURS.convert(unitValue, TimeUnit.MINUTES); unitMinute = unitValue; } private void convertMinuteToDay(long unitValue) { unitValue = TimeUnit.DAYS.convert(unitValue, TimeUnit.MINUTES); unitMinute = unitValue; } private void convertMinuteToWeek(long unitValue) { long value = unitValue /60/24/7; unitMinute = value; } public long getUnitMinute() { return unitMinute; } private void unitSelection(String from, String to, long unitValue) { if( from.equalsIgnoreCase("minute")) { if(to.equalsIgnoreCase("second")) { convertMinuteToSecond(unitValue); } else if(to.equalsIgnoreCase("hour")) { convertMinuteToHour(unitValue); } else if(to.equalsIgnoreCase("day")) { convertMinuteToDay(unitValue); } else if(to.equalsIgnoreCase("week") ) { convertMinuteToWeek(unitValue); } else { System.out.println("Invalid argument...!"); } } } }
source share