按照格式化输出时间
package pack; import java.text.SimpleDateFormat; import java.util.Date; public class Demo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Date date = new Date(); System.out.println(date); //按照格式化输出时间 SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String format = sd.format(date); System.out.println(format); } }
Calendar操作日期时间
package pack; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Demo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //获取实例 Calendar c = Calendar.getInstance(); System.out.println(c); Date time = c.getTime(); System.out.println(time); //设置calendar日期时间 c.set(2015, 07, 15); time = c.getTime(); System.out.println(time); //增加3个月 c.add(Calendar.DAY_OF_MONTH, 3); time = c.getTime(); System.out.println(time); } }
打印最近3天的时间
package pack; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * @Description TODO 打印最近3天的时间 * @author WiKi * @date 2014-11-29 上午11:31:25 */ public class Demo { /** * Calendar日期类 这个类可以把时间设置在任意时刻 * 查询最近三天的记录 */ public static void main(String[] args) { Calendar c = Calendar.getInstance(); Date nowtime; c.set(2015, 06, 25, 12, 20); nowtime = c.getTime(); SimpleDateFormat sd = new SimpleDateFormat("yyyy-M-d HH:mm:ss E"); System.out.println(sd.format(nowtime)); c.add(Calendar.DAY_OF_MONTH, -3); System.out.println(sd.format(c.getTime())); } }
时间: 2024-10-24 03:05:27