话不多说,上程序,如图:
public class TestDate { public static void main(String[] args) throws ParseException { String str = "2016-6-28 8:00:00"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //字符串转Date Date date = sdf.parse(str); //Date转字符串 str = sdf.format(date); System.out.println(date); System.out.println(str); } }
结果:
Tue Jun 28 08:00:00 CST 2016 2016-06-28 08:00:00
打印昨天的当前时刻
public class YesterdayCurrent { public static void main(String[] args){ Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); System.out.println(cal.getTime()); } }