`
tsinglongwu
  • 浏览: 233365 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Java Date一个小bug

    博客分类:
  • java
阅读更多
public static void main(String[] args) throws ParseException {
		Date date = new Date();
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
		java.util.Date beginDate = format.parse("1900-01-01");
		java.util.Date endDate = format.parse("1970-01-01");
		long d = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
		System.out.println("相隔的天数=" + d);

		
		System.out.println(secondToDate(0L));

		System.out.println(stringToDate("5", "dd"));
	}

	public final static Timestamp date2Timestamp(Date date)
			throws ParseException {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss",
				Locale.ENGLISH);
		dateFormat.setLenient(false);
		Date timeDate = dateFormat.parse(dateFormat.format(date));
		Timestamp dateTime = new Timestamp(timeDate.getTime());
		return dateTime;
	}

        //传入0按理应该输出Mon Jan 01 00:00:00 CST 1900,但却输出Mon Jan 01 08:05:52 CST 1900
	public static Date secondToDate(Long l) {
		return new Date(l * 1000 - 2208988800000L);
	}

	public static Date stringToDate(String s, String format)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(s);
	}
}


结果:
相隔的天数=25567
Mon Jan 01 08:05:52 CST 1900
Mon Jan 05 00:00:00 CST 1970



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics