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 + "分鐘"; }
?