1)如何取得年月日、小时分钟秒?
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test {
public static void main(String[] args) throws ParseException {
Calendar now = Calendar.getInstance();
System.out.println("年: " + now.get(Calendar.YEAR));
System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");
System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));
System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));
System.out.println("分: " + now.get(Calendar.MINUTE));
System.out.println("秒: " + now.get(Calendar.SECOND));
System.out.println("当前时间毫秒数:" + now.getTimeInMillis());
System.out.println(now.getTime());
Date d = new Date();
System.out.println(d);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateNowStr = sdf.format(d);
System.out.println("格式化后的日期:" + dateNowStr);
String str = "2012-1-13 17:26:33"; //要跟上面sdf定义的格式一样
Date today = sdf.parse(str);
System.out.println("字符串转成日期:" + today);
}
}
2)如何取得从1970年1月1日0时0分0秒到现在的毫秒数?
Calendar.getInstance().getTimeInMillis();
System.currentTimeMillis();
3)如何取得某月的最后一天?
Calendar time = Calendar.getInstance();
time.getActualMaximum(Calendar.DAY_OF_MONTH);
4)如何格式化日期?
利用java.text.DataFormat 的子类(如SimpleDateFormat类)中的format(Date)方法可将日期格式化。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateNowStr = sdf.format(d);
System.out.println("格式化后的日期:" + dateNowStr);
String str = "2012-1-13 17:26:33"; //要跟上面sdf定义的格式一样
Date today = sdf.parse(str);
System.out.println("字符串转成日期:" + today);
5)打印昨天的当前时刻
public class Test {
public static void main(String[] args) throws ParseException {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
System.out.println(cal.getTime());
}
}