[Java]如何获取年份~
方法一,用Calendar类,可以获取时间的年、月、日、时、分等各个部分
Calendar date = Calendar.getInstance();
String year = String.valueOf(date.get(Calendar.YEAR));
方法二,用SimpleDateFormat对日期格式化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
Date date = new Date();
String year = sdf.format(date);
java获取当前时间,将天数减一天,返回Date
要什么格式的?给你个这样的“yy-mm-dd hh-mm-ss”//根据时间字符串把天数加1 public String addOneDay(String time) { String add = null; try { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); java.util.Date timeNow = df.parse(time); Calendar begin=Calendar.getInstance(); begin.setTime(timeNow); begin.add(Calendar.DAY_OF_MONTH,1); add = df.format(begin.getTime()); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return add; }这是加1的你把1改成-1就行了获取当前时间的函数://获取当前时间 public String getTime() { TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai"); TimeZone.setDefault(tz); Calendar rightNow = Calendar.getInstance(); SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //格式大小写有区别 String sysDatetime = fmt.format(rightNow.getTime()); return sysDatetime; }