如何获取 Java 中选定日期范围的时间戳
How can I get timestamp for selected date range in Java
public class DateTime {
public static java.util.LinkedList searchBetweenDates(java.util.Date startDate, java.util.Date endDate)
{
java.util.Date begin = new Date(startDate.getTime());
java.util.LinkedList list = new java.util.LinkedList();
list.add(new Date(begin.getTime()));
while(begin.compareTo(endDate)<0)
{
begin = new Date(begin.getTime() + 86400000);
list.add(new Date(begin.getTime()));
Timestamp timestamp = new Timestamp(new Date().getTime());
int total=3;
Calendar cal = Calendar.getInstance();
for(int d=0; d<=total; d++)
{
cal.add(Calendar.MINUTE, 2);
timestamp = new Timestamp(cal.getTime().getTime());
}
System.out.println(timestamp);
}
return list;
}
public static void main( String[] args )throws Exception
{
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Enter the Start Date: dd/mm/yyyy");
String begin = new String();
begin = input.nextLine();
System.out.println("Enter the End Date: dd/mm/yyyy");
String end = new String();
end = input.nextLine();
java.util.LinkedList hitList = searchBetweenDates(
new java.text.SimpleDateFormat("dd/MM/yyyy").parse(begin),
new java.text.SimpleDateFormat("dd/MM/yyyy").parse(end));
String[] comboDates = new String[hitList.size()];
for(int i=0; i<hitList.size(); i++)
comboDates[i] = new java.text.SimpleDateFormat("dd/MM/yyyy ").format(((java.util.Date)hitList.get(i)));
for(int i=0; i<comboDates.length; i++)
System.out.println(comboDates[i]);
input.close();
}
}
我想为所选日期范围而不是当前日期打印时间戳。为了
例如,如果我选择了日期范围 从 01/01/2016 到 05/01/2016 那么输出应该是这样的:
01/01/2016 12:02:01
12:04:45
till
11:59:00
02/01/2016 12:02:01
12:04:45
截止日期之前的所有人都一样。但是通过这段代码,我得到了当前日期和当前时间戳,并且在所选日期范围的唯一日期之后没有时间戳。
您只能获得当前日期,因为 Calendar.getInstance()
returns 一个 Calendar
对象设置为当前日期。所以在该行之后,您需要使用 cal.setTime(begin)
.
将时间设置为开始日期
此外,打印时间戳必须在您的 for 循环中完成,否则只会打印当天的最后一个时间戳。
我假设您还希望将开始日期和结束日期打印为时间戳。为此,您必须将行 begin = new Date(begin.getTime() + 86400000)
移动到 while 循环的末尾,否则您将跳过开始日期。这样,您可能会看到不会打印结束日期。因此,您也将结束日期的时间设置为 while 循环前一天的结束时间。
编辑: 要将结束日期的时间设置为一天的结束,以便它也显示出来,请尝试使用 endDate.setTime(endDate.getTime() + 24*3600*1000)
。这样,日期将在未来移动一天,并且还将使用整个 endDate。请记住在 while 循环之前执行此操作。
public class DateTime {
public static java.util.LinkedList searchBetweenDates(java.util.Date startDate, java.util.Date endDate)
{
java.util.Date begin = new Date(startDate.getTime());
java.util.LinkedList list = new java.util.LinkedList();
list.add(new Date(begin.getTime()));
while(begin.compareTo(endDate)<0)
{
begin = new Date(begin.getTime() + 86400000);
list.add(new Date(begin.getTime()));
Timestamp timestamp = new Timestamp(new Date().getTime());
int total=3;
Calendar cal = Calendar.getInstance();
for(int d=0; d<=total; d++)
{
cal.add(Calendar.MINUTE, 2);
timestamp = new Timestamp(cal.getTime().getTime());
}
System.out.println(timestamp);
}
return list;
}
public static void main( String[] args )throws Exception
{
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Enter the Start Date: dd/mm/yyyy");
String begin = new String();
begin = input.nextLine();
System.out.println("Enter the End Date: dd/mm/yyyy");
String end = new String();
end = input.nextLine();
java.util.LinkedList hitList = searchBetweenDates(
new java.text.SimpleDateFormat("dd/MM/yyyy").parse(begin),
new java.text.SimpleDateFormat("dd/MM/yyyy").parse(end));
String[] comboDates = new String[hitList.size()];
for(int i=0; i<hitList.size(); i++)
comboDates[i] = new java.text.SimpleDateFormat("dd/MM/yyyy ").format(((java.util.Date)hitList.get(i)));
for(int i=0; i<comboDates.length; i++)
System.out.println(comboDates[i]);
input.close();
}
}
我想为所选日期范围而不是当前日期打印时间戳。为了 例如,如果我选择了日期范围 从 01/01/2016 到 05/01/2016 那么输出应该是这样的:
01/01/2016 12:02:01
12:04:45
till
11:59:00
02/01/2016 12:02:01
12:04:45
截止日期之前的所有人都一样。但是通过这段代码,我得到了当前日期和当前时间戳,并且在所选日期范围的唯一日期之后没有时间戳。
您只能获得当前日期,因为 Calendar.getInstance()
returns 一个 Calendar
对象设置为当前日期。所以在该行之后,您需要使用 cal.setTime(begin)
.
此外,打印时间戳必须在您的 for 循环中完成,否则只会打印当天的最后一个时间戳。
我假设您还希望将开始日期和结束日期打印为时间戳。为此,您必须将行 begin = new Date(begin.getTime() + 86400000)
移动到 while 循环的末尾,否则您将跳过开始日期。这样,您可能会看到不会打印结束日期。因此,您也将结束日期的时间设置为 while 循环前一天的结束时间。
编辑: 要将结束日期的时间设置为一天的结束,以便它也显示出来,请尝试使用 endDate.setTime(endDate.getTime() + 24*3600*1000)
。这样,日期将在未来移动一天,并且还将使用整个 endDate。请记住在 while 循环之前执行此操作。