使用vincent robert的答案,我想用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的 scanf("%d/%d/%d", &mon, &day, &year);
. 如何使用一行代码在java中实现类似的功能?
我期待这样的信息:
8/24/1995 12:30PM
%d/%d/%d %d/%d%cM
3条答案
按热度按时间sqougxex1#
这是有效的,尽管你需要添加另一个
/
年后的性格。例如:
尽管如此,java不是c(c也不是java)。试着用java和用c完全一样的方式编写代码就像从左到右写阿拉伯语一样。java中的just use方法
nextLine()
读取整行输入,然后解析该输入。a6b3iqyw2#
使用现代日期时间api和
String#format
,您可以按以下步骤操作:示例运行:
在trail:date-time了解有关现代日期时间api的更多信息。
tvz2xvvm3#
一种可能的方法是: