在 Parse 后端查找当前时间和上次更新时间之间的差异
Finding the difference between current time and last updated time on Parse backend
我正在尝试编写一个 Java 脚本来计算当前日期与存储在我们的 Parse 后端的最后更新时间之间的时差。谁能帮我找出代码中的错误?你可能认为这还不错,但我已经查看 Stack Overflow 数小时但无济于事。
//Create a date formatter.
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'LLL'Z'");
//Create a date object for today's date.
Date currentDate=new Date();
//Create a string for the date from parse.
String parseTime = singleClaim.getString("updatedAt");
//Create a string for the current date.
String currentTime=currentDate.toString();
//Initialize the date object for the updatedAt time on Parse.
Date parseDate = null;
//Initialize the date object for the current time.
Date FormattedCurrentDate = null;
try {
//Here, we convert the parseTime string into a date object and format it.
parseDate = formatter.parse(parseTime);
//Here, we convert the currentTime string into a date object and format it.
FormattedCurrentDate = formatter.parse(currentTime);
}
catch (Exception e) {
e.printStackTrace();
}
//Get the time difference from the current date versus the date on Parse.
long difference = FormattedCurrentDate.getTime()-parseDate.getTime();
您不应调用 toString() 将 Date 转换为 String。为什么要将 currentTime 转换为 String 然后再将其解析为日期?这毫无意义。
//Create a date formatter.
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'LLL'Z'");
//Create a date object for today's date.
Date currentDate=new Date();
//Create a string for the date from parse.
String parseTime = singleClaim.getString("updatedAt");
//Create a string for the current date.
String currentTime=currentDate.toString();
//Initialize the date object for the updatedAt time on Parse.
Date parseDate = null;
try {
//Here, we convert the parseTime string into a date object and format it.
parseDate = formatter.parse(parseTime);
}
catch (Exception e) {
e.printStackTrace();
}
//Get the time difference from the current date versus the date on Parse.
long difference = currentDate.getTime()-parseDate.getTime();
这两行的组合可能导致了您的问题:
String currentTime=currentDate.toString();
// ...
FormattedCurrentDate = formatter.parse(currentTime);
currentTime
变量不包含格式化程序可以使用的格式正确的字符串。
我也觉得没有必要创建这样的字符串,您可以按如下方式创建:
long difference = currentDate.getTime() - parseDate.getTime();
如果您绝对坚持从日期到字符串再返回,则必须按如下方式创建 currentTime
字符串:
currentTime = formatter.format(currentDate);
我正在尝试编写一个 Java 脚本来计算当前日期与存储在我们的 Parse 后端的最后更新时间之间的时差。谁能帮我找出代码中的错误?你可能认为这还不错,但我已经查看 Stack Overflow 数小时但无济于事。
//Create a date formatter.
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'LLL'Z'");
//Create a date object for today's date.
Date currentDate=new Date();
//Create a string for the date from parse.
String parseTime = singleClaim.getString("updatedAt");
//Create a string for the current date.
String currentTime=currentDate.toString();
//Initialize the date object for the updatedAt time on Parse.
Date parseDate = null;
//Initialize the date object for the current time.
Date FormattedCurrentDate = null;
try {
//Here, we convert the parseTime string into a date object and format it.
parseDate = formatter.parse(parseTime);
//Here, we convert the currentTime string into a date object and format it.
FormattedCurrentDate = formatter.parse(currentTime);
}
catch (Exception e) {
e.printStackTrace();
}
//Get the time difference from the current date versus the date on Parse.
long difference = FormattedCurrentDate.getTime()-parseDate.getTime();
您不应调用 toString() 将 Date 转换为 String。为什么要将 currentTime 转换为 String 然后再将其解析为日期?这毫无意义。
//Create a date formatter.
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'LLL'Z'");
//Create a date object for today's date.
Date currentDate=new Date();
//Create a string for the date from parse.
String parseTime = singleClaim.getString("updatedAt");
//Create a string for the current date.
String currentTime=currentDate.toString();
//Initialize the date object for the updatedAt time on Parse.
Date parseDate = null;
try {
//Here, we convert the parseTime string into a date object and format it.
parseDate = formatter.parse(parseTime);
}
catch (Exception e) {
e.printStackTrace();
}
//Get the time difference from the current date versus the date on Parse.
long difference = currentDate.getTime()-parseDate.getTime();
这两行的组合可能导致了您的问题:
String currentTime=currentDate.toString();
// ...
FormattedCurrentDate = formatter.parse(currentTime);
currentTime
变量不包含格式化程序可以使用的格式正确的字符串。
我也觉得没有必要创建这样的字符串,您可以按如下方式创建:
long difference = currentDate.getTime() - parseDate.getTime();
如果您绝对坚持从日期到字符串再返回,则必须按如下方式创建 currentTime
字符串:
currentTime = formatter.format(currentDate);