java - SimpleDateFormat 返回不同的结果
问题描述
我正在尝试将 a 解析string
为 a SimpleDateFormat
,但是它不显示yyyy-MM-dd
我制作的格式。
这是结果:
2019 年 6 月 23 日星期日 00:00:00 GMT+08:00
这就是我需要的:
2019-07-23
这是我的代码:
onDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
String date = year+"-"+month+"-"+dayOfMonth;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
try {
Date datex = format.parse(date);
System.out.println(datex);
} catch (ParseException e) {
e.printStackTrace();
}
}
};
解决方案
通过以下更简单的方法来做到这一点,
String date = year+"-"+month+"-"+dayOfMonth;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
try {
Date datex = format.parse(date);
String outputDate = format.format(datex);
System.out.println(outputDate);
} catch (ParseException e) {
e.printStackTrace();
}
推荐阅读
- r - 日志文件中的并发用户数
- php - Symfony 5 - 无法启动会话,因为标头已由“php://input”在生产中的第 1 行发送
- javascript - 如何使用javascript在html标签末尾添加文本
- r - 如何防止 geom_segment 中的重叠段
- ansible - Ansible中如何使用lookup插件获取目录路径和文件
- r-markdown - 在 rmarkdown 中使用 \newenvironment
- c# - C# 属性,通过接口访问时返回接口类型,通过具体类访问时返回具体类型
- javascript - 为什么我在 react redux 中的更新会使用我所做的更新而不是编辑它来创建新数据?
- python - Django - 有没有办法在事务提交之前验证模型及其相关模型?
- zsh - 将命令分配给 Zsh 中的变量