js,java时间处理
生活随笔
收集整理的這篇文章主要介紹了
js,java时间处理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.JS獲取時間格式為“yyyy-MM-dd HH:mm:ss”的字符串
function getTimeStr(){var myDate = new Date();var year = myDate.getFullYear(); //獲取完整的年份(4位,1970-????)var month = myDate.getMonth(); //獲取當前月份(0-11,0代表1月)month = month > 9 ? month : "0"+month;var day = myDate.getDate(); //獲取當前日(1-31)day = day > 9 ? day : "0"+day;var hours = myDate.getHours(); //獲取當前小時數(0-23)hours = hours > 9 ? hours : "0"+hours;var minutes = myDate.getMinutes(); //獲取當前分鐘數(0-59)minutes = minutes > 9 ? minutes : "0"+minutes;var seconds = myDate.getSeconds(); //獲取當前秒數(0-59)seconds = seconds > 9 ? seconds : "0"+seconds;return year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;}
2.java字符串轉時間“字符串可以是多種格式”
public static void main(String[] args) {String strDate="2005年04月22日";//注意:SimpleDateFormat構造函數的樣式與strDate的樣式必須相符SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd日 ");SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //加上時間//必須捕獲異常try {Date date=simpleDateFormat.parse(strDate);System.out.println(date);} catch(ParseException px) {px.printStackTrace();}}3.java 計算兩個時間之間的差多久
public static String getDatePoor(Date endDate, Date nowDate) {long nd = 1000 * 24 * 60 * 60;long nh = 1000 * 60 * 60;long nm = 1000 * 60;// long ns = 1000;// 獲得兩個時間的毫秒時間差異long diff = endDate.getTime() - nowDate.getTime();// 計算差多少天long day = diff / nd;// 計算差多少小時long hour = diff % nd / nh;// 計算差多少分鐘long min = diff % nd % nh / nm;// 計算差多少秒//輸出結果// long sec = diff % nd % nh % nm / ns;return day + "天" + hour + "小時" + min + "分鐘"; }?
轉載于:https://www.cnblogs.com/UncleWang001/p/9831458.html
總結
以上是生活随笔為你收集整理的js,java时间处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Taro+react开发(79):tar
- 下一篇: [react] react16跟之前的版