我想创建一个Date对象,将其经过时间设置为10000,100000,1000000,10000000,1000000,1000000000,1000000000000和100000000000毫秒,并使用toString()方法显示日期和时间。 但我不知道如何创建一个以增加的毫秒值管理的for循环?
这是我到目前为止所做的:
public class Date {
public static void main(String[] args) {
long i = 0;
java.util.Date date = new java.util.Date(i);
date.setTime(i);
for (i = 1000; i < 100000000000L; i *= 10) {
System.out.println("Time elapsed: " + i + " milliseconds");
}
System.out.println("Date and time: " + date.toString());
}
}
答案 0 :(得分:4)
只需将日期和toString放入for循环
即可 long i = 0;
java.util.Date date = new java.util.Date(i);
for (i = 1000; i < 100000000000L; i *= 10) {
date.setTime(i);
System.out.println("Time elapsed: " + i + " milliseconds");
System.out.println("Date and time: " + date.toString());
}
}
答案 1 :(得分:1)
import java.util.Date; // 1
class FoobarTimeMachine {
public static void main(String[] args) {
Date date; // 2
for(long i = 1000l; i <= 100000000000l; i *= 10) { // 3
System.out.println("Time elapsed since epoch: " + i + " milliseconds");
date = new Date(i); // 4
System.out.println("Corresponding date: " + date); // 5
}
}
}
评论:
java.util.Date
,因此我们稍后可以Date
使用它。<=
所以我们可以达到100 000 000 000。someString + someObject
=&gt; someString + someObject.toString()
答案 2 :(得分:0)
public static void main(String[] args) {
long i = 0;
Date d=new Date(i);
for (i = 1000; i < 100000000000L; i *= 10) {
System.out.println("Time elapsed: " + i + " milliseconds");
d.setTime(i);
System.out.println("Date and time: " + d.toString());
}
}