只有年份的java中的日期

Ank*_*gra 2 java format date

我的目标是以"{current year} -01-01 00:00:00"的格式获取日期,即只有年份的值随时间变化.什么是最好的方法.

运用

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-01-01 00:00:00");
String format = simpleDateFormat.format(new Date());
Run Code Online (Sandbox Code Playgroud)

看起来不够干净.我还有其他选择吗?

RMT*_*RMT 5

您应该使用Calendar对象.更容易管理,如果你需要到达date那里有方法.

  Calendar cal = Calendar.getInstance();
 //Set to whatever date you want as default
  cal.set(Calendar.YEAR, *year you want*);
Run Code Online (Sandbox Code Playgroud)