public static void main(String[] arg0){ Date date = new Date(); System.out.println(date); //Mon Aug 13 09:53:31 CST 2018 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String s = format.format(date); System.out.println(s); //2018-08-13 09:53:31 //对其中年,月,日.时,分,秒 进行操作 Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH )+1; //默认1月为0月 0-11 int da= c.get(Calendar.DATE); int hour = c.get(Calendar.HOUR_OF_DAY); int mm = c.get(Calendar.MINUTE); int second = c.get(Calendar.SECOND); int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);//默认星期日为一个星期的第一天 int dayOfMonth = c.get(Calendar.DAY_OF_MONTH); //一个月的第几天 int actualMaximum = c.getActualMaximum(Calendar.DAY_OF_MONTH); //获得一个月的最大的天数 int weekOfMonth = c.get(Calendar.WEEK_OF_MONTH);//月里的第几周 System.out.println(year +"/"+month+"/"+da+" "+hour+":"+mm+":"+second); System.out.println("星期" + (dayOfWeek-1) + " 一个月的第" + dayOfMonth +"天 这个月一共有" +actualMaximum+"天 是这个月的第"+weekOfMonth + "周" ); }
打印结果
Mon Aug 13 10:25:35 CST 2018
2018-08-13 10:25:35
2018/8/13 10:25:35
星期1 一个月的第13天 这个月一共有31天 是这个月的第3周
String类型转换为时间格式
public static void main(String[] arg0) throws ParseException { Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String s = format.format(date); Date date1 = format.parse(s); System.out.println(date1); }