我正在使用SimpleDateFormat类来获取这样的当前年份
SimpleDateFormat currentYear = new SimpleDateFormat("YYYY");
Date thisYear = new Date();
String stringThisYear = currentYear .format(thisYear );
Run Code Online (Sandbox Code Playgroud)
它在这里(印度)工作正常,但是在其他人在马来西亚运行时崩溃了。经过大量调试和搜索,我发现由于没有提及语言环境而发生了问题,因此我将代码更改为此
SimpleDateFormat currentDate = new SimpleDateFormat("YYYY", Locale.ENGLISH);
Run Code Online (Sandbox Code Playgroud)
仍然很崩溃,所以我使用日历实例来获取这样的当前年份
int thisYear=Calendar.getInstance().get(Calendar.YEAR);
Run Code Online (Sandbox Code Playgroud)
但是我想知道simpleDateFormat是什么问题,因为我的大多数代码已经具有simpleDateFormat类。所以我想知道崩溃的原因,如果有人有解决方案,请帮助我。谢谢
我正在尝试实现一个 JPA 查询来检查是否存在与当前年月匹配的日期时间戳记录。现在我正在获取所有记录并进行迭代以匹配。我知道这不是正确的实现,只是想知道是否有任何内置的 JPA 查询可用于这种情况。
这是我到目前为止的实现
List<Product> productList= productRepository.findAll();
/*getMonthInt() and getYearInt() is the custom function which returns current
month and year. As date.getMonth() and date.getYear() is deprecated
*/
int currentMonth=getMonthInt(new Date());
int currentYear=getYearInt(new Date());
for(Product product:productList){
int fetchedMonth=getMonthInt(product.getShipmentDate()) ;
int fetchedYear=getYearInt(product.getShipmentDate());
if(fetchedMonth == currentMonth && fetchedYear == currentYear){
//record exist
}
else{
//do something else
}
}
Run Code Online (Sandbox Code Playgroud)