datetime格式化

org.joda.DateTime格式化成特定的String非常容易,因为它重写了toString方法,而且接受一个格式参数。

不仅如此,它还支持超强的日期运算。

看代码:

public static String getLastExpectNum(){
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM");
		return DateTime.now().minusMonths(2).toString(formatter);
	}

这段代码运行后将会把当前时间先往前减掉2个月,再用yyyy-MM的格式输出出来。

猜你喜欢

转载自wandejun1012.iteye.com/blog/2277136