我正在使用一个实时项目,我需要在1970年1月1日之后找到秒数.我使用下面的代码来查找秒数,但是给出了错误的结果.代码如下.
public long returnSeconds(int year, int month, int date) {
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
calendar1.set(1970, 01, 01);
calendar2.set(year, month, date);
long milliseconds1 = calendar1.getTimeInMillis();
long milliseconds2 = calendar2.getTimeInMillis();
long diff = milliseconds2 - milliseconds1;
long seconds = diff / 1000;
return seconds;
}
Run Code Online (Sandbox Code Playgroud)
在上面代替year,month,date我通过2011,10,1和我得到
1317510000
Run Code Online (Sandbox Code Playgroud)
但正确的答案是
1317427200
Run Code Online (Sandbox Code Playgroud)
对此有任何帮助对我非常有用.
我在下面编写了正在运行并提供输出的代码。但是我不确定这是正确的。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = new Date();
sdf.setTimeZone(TimeZone.getTimeZone("GMT-7"));
String value = sdf.format(date);
System.out.println(value);
Date date2 = sdf.parse(value);
long result = date2.getTime();
System.out.println(result);
return result;
Run Code Online (Sandbox Code Playgroud)
上面的代码我正在尝试的是,我只需要获取GMT时区的当前时间并将其转换为将在Oracle db中使用的纪元格式即可。
有人可以告诉我这种格式,并且上面的代码正确吗?