android 获取系统时间的时间戳 ,时间戳日期互转,计算日期时间差,获取明天日期,比较时间大小
?
?
long creatTime=mBindCheck.remainPaymentTime; int shi = (int)mBindCheck.remainPaymentTime/(3600*1000); int yu1 = (int)mBindCheck.remainPaymentTime%(3600*1000); int fen = (int)yu1/(60*1000); int yu2 = (int)yu1%(60*1000); int mia = (int)yu2/1000;?獲取系統時間戳
public String getTime(){long time=System.currentTimeMillis()/1000;//獲取系統時間的10位的時間戳String str=String.valueOf(time);return str; }點擊時間差
private long mTouchDownTime;//1000為1秒 mTouchDownTime = SystemClock.uptimeMillis(); if (SystemClock.uptimeMillis() - mTouchDownTime < RANGE_TOUCH_MOVE_INTERVAL) {return; }、獲取系統時間
? SimpleDateFormat df = new SimpleDateFormat("HH");//設置日期格式"yyyy年-MM月dd日-HH時mm分ss秒"String dfd = df.format(new Date());
結果如下
2017年-05月26日-14時49分29秒
時間戳轉換日期
public static String timetodate(String time) {Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(Long.valueOf(time));SimpleDateFormat sf = new SimpleDateFormat("MM-dd ");//這里的格式可換"yyyy年-MM月dd日-HH時mm分ss秒"等等格式String date = sf.format(calendar.getTime());return date;}時間日期轉換成時間戳
/** 將時間轉換為時間戳*/ public static String dateToStamp(String s) throws ParseException {String res;SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = simpleDateFormat.parse(s);long ts = date.getTime();res = String.valueOf(ts);return res; } /*** 四舍五入到小數點后scale位* @param v* @param scale* @return*/ public static float round(float v, int scale) {if (scale < 0)throw new IllegalArgumentException("The scale must be a positive integer or zero");BigDecimal bgNum1 = new BigDecimal(Float.toString(v));BigDecimal bgNum2 = new BigDecimal("1");return bgNum1.divide(bgNum2, scale, BigDecimal.ROUND_HALF_UP).floatValue();// return b.setScale(scale, BigDecimal.ROUND_HALF_UP).floatValue(); }轉換日期時間格式
public static String getDistanceTimeToHM(String str1) {DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String typestring = "yyyy-MM-dd HH:mm";String endtime="";Date one;try {one = df.parse(str1);endtime = dateToString(one,typestring);} catch (ParseException e) {e.printStackTrace();}return endtime; } public static String dateToString(Date data, String formatType) {return new SimpleDateFormat(formatType).format(data); }如2017-11-01 09:16:03.0轉換后是2017-11-01 09:16
計算兩個日期時間差,天數,時、分、秒、
public int getDistanceTime(String end, String start) {DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date one;Date two;long day = 0;long hour = 0;long min = 0;long sec = 0;try {final Calendar c = Calendar.getInstance();c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));one = df.parse(end);c.setTime(one);two = df.parse(start);long time1 = one.getTime();long time2 = two.getTime();long diff ;diff = time1 - time2;day = diff / (24 * 60 * 60 * 1000);//天數Log.i("lgq","tian--==="+day+"...one="+end+"..-----.two==="+start);hour = (diff / (60 * 60 * 1000) - day * 24);//時min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);//分sec = ((diff/1000)- day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);} catch (ParseException e) {e.printStackTrace();}return (int)sec; }獲取明天日期
?//獲得明天日期
? ?? ?private String getTomoData(){
? ?? ??? ?
? ?? ?// 添加大小月月份并將其轉換為list,方便之后的判斷
?? ??? ?String[] months_big = { "1", "3", "5", "7", "8", "10", "12" };
?? ??? ?String[] months_little = { "4", "6", "9", "11" };
?
?? ??? ?List<String> list_big = Arrays.asList(months_big);
?? ??? ?List<String> list_little = Arrays.asList(months_little);
? ?? ??? ?
? ?? ??? ?Calendar calendar = Calendar.getInstance();
? ?? ??? ?int year = calendar.get(Calendar.YEAR);
? ?? ??? ?int month = calendar.get(Calendar.MONTH)+1;
? ?? ??? ?int day = calendar.get(Calendar.DATE);
? ?? ??? ?if(day==30){
? ?? ??? ??? ?if (list_big.contains(String.valueOf(month))){
? ?? ??? ??? ??? ?day=31;
? ?? ??? ??? ?}
? ?? ??? ??? ?if (list_little.contains(String.valueOf(month))){
? ?? ??? ??? ??? ?day=1;
? ?? ??? ??? ??? ?if(month==12){
? ?? ??? ??? ??? ??? ?year++;
? ?? ??? ??? ??? ??? ?month=1;
? ?? ??? ??? ??? ?}else{
? ?? ??? ??? ??? ??? ?month++;
? ?? ??? ??? ??? ?}
? ?? ??? ??? ??? ?
? ?? ??? ??? ?}
? ?? ??? ?}else if(day==31){
? ?? ??? ??? ?day=1;
? ?? ??? ??? ?if(month==12){
? ?? ??? ??? ??? ?year++;
? ?? ??? ??? ??? ?month=1;
? ?? ??? ??? ?}else{
? ?? ??? ??? ??? ?month++;
? ?? ??? ??? ?}
? ?? ??? ??? ?
? ?? ??? ?}else {
? ?? ??? ??? ?day++;
? ?? ??? ?}
? ?? ? ? ?String data;
? ?? ? ? ?if( day<10)
? ?? ? ? ?{
? ?? ? ? ??? ?
? ?? ? ? ??? ? data=year+"-"+"0"+month+"-"+"0"+day;
? ?? ? ? ?}
? ?? ? ? ?else
? ?? ? ? ?{
? ?? ? ? ??? ?data=year+"-"+"0"+month+"-"+day;
?? ??? ?}
?? ??? ?
? ?? ??? ?return data;
? ?? ?}
比較時間大小/
/*** 對比兩個時間** @param startTimedama* @param endTime* @return*/ public static boolean getTimeCompareSize(String startTimedama, String endTime) {int i = 0;SimpleDateFormat dateFormat = new SimpleDateFormat("MM月dd日 HH:mm:ss");//年-月-日 時-分try {Date date1 = dateFormat.parse(startTime);//開始時間Date date2 = dateFormat.parse(endTime);//結束時間// 1 結束時間小于開始時間 2 開始時間與結束時間相同 3 結束時間大于開始時間if (date2.getTime() < date1.getTime()) {i = 1;} else if (date2.getTime() == date1.getTime()) {i = 2;} else if (date2.getTime() > date1.getTime()) {//正常情況下的邏輯操作.i = 3;}} catch (ParseException e) {e.printStackTrace();}if (i == 1 || i == 2) {return true;} else {return false;} }總結
以上是生活随笔為你收集整理的android 获取系统时间的时间戳 ,时间戳日期互转,计算日期时间差,获取明天日期,比较时间大小的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信内置浏览器不支持下载文件的解决方案
- 下一篇: mongoose 联表、及联查询 使用p