默认日期年份设定为1970年 [英] Default Date year set at 1970

查看:1106
本文介绍了默认日期年份设定为1970年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析一个原本没有设置年份的字符串,如
13 Aug 11:30

I need to parse a String that does not originally have the year set like "13 Aug 11:30"

但是当我输出日期时它增加了一年,
我尝试设定年份,但年度完全错误输出为Thu Aug 13 11:30:00 GMT 3911

but when I output the date it adds the year on, I tried set year but the year is totally wrong the output comes as "Thu Aug 13 11:30:00 GMT 3911"

有没有办法在解析日期后设置年份?

Is there a way to set the year after the date is parsed?

SimpleDateFormat formatter = new SimpleDateFormat("dd MMM HH:mm");

String dateStr = "13 Aug 11:30";
Date fromDate = (Date)formatter.parse(dateStr);


fromDate.setYear(2011);


推荐答案

使用日历设置年份:

Calendar c = Calendar.getInstance();
c.setTime(fromDate);
c.set(Calendar.YEAR, 2011);
fromDate = c.getTime();

这篇关于默认日期年份设定为1970年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆