0
Я пытаюсь рассчитать разницу во времени между датой установки пользователя и текущей системой-датой.Разница во времени между событиями
Мой код не так,
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args){
// Make the frame
JFrame appFrame = new JFrame("Get Things Done");
// Make a panel
WelcomePanel buttonPanel = new WelcomePanel();
// Set the class button to work
appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add the panel to the frame
appFrame.add(buttonPanel);
// Set the size of the frame
appFrame.setSize(400, 400);
// Make the frame visible
appFrame.setVisible(true);
appFrame.pack();
WelcomePanel welcome = new WelcomePanel();
// --------------------------------------------------------------------------
// Print currentTime
Time time = new Time();
time.printCurrentTime();
UserEvent input = new UserEvent();
// Test inputTime
input.setInputTime("15/05/28 18:00:00");
System.out.println("***Test Getters and Setters***");
System.out.println(input.getInputTime());
} // end main
} // end class
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Time {
Date currentDate = new Date();
UserEvent user = new UserEvent();
// Print current time
public void printCurrentTime(){
DateFormat dateFormat = new SimpleDateFormat ("yy/MM/dd HH:mm:ss");
// System.out.println("date without format: "+ currentDate.toString());
System.out.println(dateFormat.format(currentDate));
} // end printCurrentTime
public long getCurrentTime(){
return currentDate.getTime();
}
// ----------------------------------------------------------------------------
} // end class
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class UserEvent {
private String inputTime;
Date user = new Date();
// set inputTime
public void setInputTime(String newInputTime){
inputTime = newInputTime;
} // end setInputTime
// get inputTime
public String getInputTime(){
return inputTime;
} // end getInputTime
public void parseInput(){
DateFormat dateFormat = new SimpleDateFormat ("yy/MM/dd HH:mm:ss");
try{
user = dateFormat.parse(inputTime);
System.out.println(user);
} catch (ParseException error){
System.out.println("Are you sure you entered a date?");
} // end try/catch
}
// ---------------------------------------------------------------------------
}
Я искал много и много примеров, но до сих пор никакого прогресса в моем коде.
Где мой код для расчета разницы? Или - где это должно быть и какие даты следует различать? –
В зависимости от результата, которого вы хотите, существует ряд способов достижения этого, но вы должны либо использовать Java 8 Time API, либо JodaTime для выполнения основной работы, например [http://stackoverflow.com/questions/12851934/how-to-find-difference-between-two-joda-time-datetimes-in-minutes/12852021 # 12852021), [пример] (http://stackoverflow.com/questions/13328912/java- время ожидания/13329218 # 13329218), [пример] (http://stackoverflow.com/questions/28979880/about-days-between-two-dates/28980026#28980026) – MadProgrammer
[пример] (http: // stackoverflow.com/questions/29737159/how-can-i-create-a-calculation-to-get-the-age-of-a-person-from-two-dates/29738430#29738430) – MadProgrammer