import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Currency;
import java.util.Date;
public class test {
public static void main(String[] args) throws ParseException {
String str = "2013-5-24";
// 引用SimpleDateFormat格式化日期
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
Date date2 = sdf2.parse(str);
System.out.println(date2);
//以上是将一个字符串转换为一个日期
System.out.println("----------------------------------------------------");
// 使用SimpleDateFormat格式化日期
// yyyy-MM-dd hh:mm:ss
// yyyy年MM月dd日 HH时mm分ss秒
Date now = new Date();// 获取当前日期;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH时mm分ss秒SSS毫秒");
String strTime = sdf.format(now);
System.out.println(strTime);
System.out.println("-----------------------------------------------------");
Calendar calendar = Calendar.getInstance();//Calendar的中文翻译是日历
calendar.set(2012, 4, 6);
System.out.println( calendar.get(Calendar.WEEK_OF_YEAR));
//获取2012年4月6日共有多少个星期
System.out.println("-----------------------------------------------------");
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
//比当前月份少1
int month = cal.get(Calendar.MONTH);
//date表示日期,day表示天数,所以date与day_of_month相同
int date = cal.get(Calendar.DATE);
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
//表示本周的第几天,从周日开始计算,中国的话要减一
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
int dayOfYear = cal.get(Calendar.DAY_OF_YEAR);
//12小时制
int hour = cal.get(Calendar.HOUR);
//24小时制
int hourOfDay = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
int millisecond = cal.get(Calendar.MILLISECOND);
int maxDate = cal.getActualMaximum(Calendar.DATE);
System.out.println("现在的年份为:" + year);
System.out.println("现在的月份为:" + month);
System.out.println("现在的号为:" + date);
System.out.println("现在的号为:" + dayOfMonth);
System.out.println("现在是星期:" + dayOfWeek);
System.out.println("现在过了的天数为:" + dayOfYear);
System.out.println("现在几点:" + hour);
System.out.println("现在几点:" + hourOfDay);
System.out.println("现在几分:" + minute);
System.out.println("现在几秒:" + second);
System.out.println("现在几毫秒:" + millisecond);
System.out.println("本月最后一天是:" + maxDate);
}
}
java日期函数操作
最新推荐文章于 2021-02-12 11:46:32 发布