Date date = new Date();
System.out.println(date);
//按本地时区输出当前日期
System.out.println(date.toLocaleString());
//日期转换类
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//日期转字符串
String date1 = sdf.format(date);
System.out.println(date1);
//字符串转日期
Date date3 = sdf.parse(date1);
System.out.println(date3.toLocaleString());
System.out.println("================");
//日历
Calendar now = Calendar.getInstance();
int y = date.getYear();
System.out.println(y);
int m = date.getMonth();
System.out.println(m);
int da = date.getDay();
System.out.println(da);
int h = date.getHours();
System.out.println(h);
int d = date.getMinutes();
System.out.println(d);
int s = date.getSeconds();
System.out.println(s);
//根据日期相减求年龄
String date2 = "2018/05/01";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd");
Date date4 =sdf1.parse(date2);
int age = date.getYear()-date4.getYear()-((date.getMonth()<=date4.getMonth() && date.getDay()<date4.getDay())?1:0);
System.out.println(age);
web项目中页面时间展示
<script type="text/javascript">
function time(){
var t = document.getElementById("time");
var time = new Date();
var h = time.getHours()>9?time.getHours():"0"+timeHours();
var m = time.getMinutes()>9?time.getMinutes():"0"+time.getMinutes();
var s = time.getSeconds()>9?time.getSeconds():"0"+time.getSeconds();
t.innerHTML= h+":"+m+":"+s;
}
$(function(){
setInterval("time()",1000);
})
</script>
<body>
<h1><span id="time"></span></h1>
</body>
倒计时
<script type="text/javascript">
function time(timer){
//获取当前时间
var nd = new Date();
//获取放假时间
var yd = new Date(timer);
//求差
var date = yd - nd;
//天数
var day = Math.floor(date/1000/60/60/24);
//小时数
var hours = Math.floor(date/1000/60/60%24);
//分钟数
var minutes = Math.floor(date/1000/60%60);
//秒数
var sec = Math.floor(date/1000%60);
//毫秒
var hm = Math.floor(date%1000);
var str = "距离放假还有"+day+"天"+hours+"小时"+minutes+"分钟"+sec+"秒"+hm+"毫秒!!!";
document.getElementById("time").innerHTML=str;
}
setInterval("time('2019-10-10 8:30:00'),1000");
</script>
<body>
<h1 id="time"></h1>
</body>
日期格式 计算简单整理
最新推荐文章于 2022-11-28 21:54:00 发布