使用文森特罗伯特的回答,我想用 Java 重建它。
我是这样开始的:
// This is a draft of the program.
import java.time.LocalDateTime;
import java.util.Scanner;
import java.lang.*;
public class Test
{
public String calcRelTime(int past_month, int past_day, int past_year)
{
// initialization of time values
int minute = 60;
int hour = 60 * minute;
int day = 24 * hour;
int week = 7 * day;
int month = (365.25 * day) / 12;
int year = 12 * month;
// main part of function
LocalDateTime present = LocalDateTime.now();
/*
Something similar to:
var ts = new TimeSpan(DateTime.UtcNow.Ticks - yourDate.Ticks);
double delta = Math.Abs(ts.TotalSeconds);
*/
// the if-condition statement similar to the answer, but I won't write it down here as it is too much code
}
public static void main(String[] args)
{
Scanner input;
int mon, day, year;
System.out.println("Enter date: ");
// the line where I take input of the date from the user
System.out.println("That was " + calcRelTime(mon, day, year) + ", fellow user.");
}
}
现在在main()
函数中,我想做一些与 C's 类似的事情scanf("%d/%d/%d", &mon, &day, &year);
。如何使用单行在 Java 中实现类似的东西?
我期待这种输入:
8/24/1995 12:30PM
%d/%d/%d %d/%d%cM