如何用Android写一个时间戳编码程序‘
这是我项目中正在用的时间戳,没经过整理,你看下
package com.tianwei.utils;
import android.net.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* Created by GT on 2017/8/22.
* 注:Uinix和Windows时间不同
*/
public class Time {
public void Time() {
}
//格式时间
public static String systemTime(String time) {
SimpleDateFormat sDateFormat = null;
if (time != null && time.length() > 0) {
sDateFormat = new SimpleDateFormat(time);
} else {
sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
}
String date = sDateFormat.format(new java.util.Date());
return date;
}
//无格式时间
public static String systemTime() {
SimpleDateFormat sDateFormat = null;
sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String date = sDateFormat.format(new java.util.Date());
return date;
}
/*
* 将时间戳转换为时间
*/
public static String stampToDate(String s, String time) {
String res;
SimpleDateFormat simpleDateFormat;
if (time == null && time.length() > 0) {
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
} else {
simpleDateFormat = new SimpleDateFormat(time);
}
long lt = new Long(s);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
return res;
}
/*
* 将时间转换为时间戳
*/
public static String dateToStamp(String s) throws ParseException {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = simpleDateFormat.parse(s);
} catch (java.text.ParseException e) {
e.printStackTrace();
}
long ts = date.getTime();
res = String.valueOf(ts);
return res;
}
/**
* 系统时间戳
*/
public static long dataStampDate() {
long s = System.currentTimeMillis();
// long s = new Date().getTime();
// long s = Calendar.getInstance().getTimeInMillis();
return s;
}
/**
* Unix
* 时间戳转换成日期格式
*
* @param timestampString
* @param formats
* @return
*/
public static String timeStampUnixDate(String timestampString, String formats) {
Long timestamp = Long.parseLong(timestampString) * 1000;
String date = new java.text.SimpleDateFormat(formats).format(new java.util.Date(timestamp));
return date;
}
/**
* Unix
* 日期格式字符串转换成时间戳
*
* @param dateStr 字符串日期
* @param format 如:yyyy-MM-dd HH:mm:ss
* @return
*/
public static String dateUinxTimeStamp(String dateStr, String format) {
try {
SimpleDateFormat sdf = null;
if (format != null && format.length() > 0) {
sdf = new SimpleDateFormat(format);
} else {
sdf = new SimpleDateFormat("yyyyMMddhhmmss");
}
return String.valueOf(sdf.parse(dateStr).getTime() / 1000);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
/**
* 两个时间间的时间戳计算函数
*
* @param beginDate
* @param endDate
* @param f 时间差的形式0:秒,1:分种,2:小时,3:天
* @return long 秒
*/
public static long getDifference(Date beginDate, Date endDate, int f) {
long result = 0;
if (beginDate == null || endDate == null) {
return 0;
}
try {
// 日期相减获取日期差X(单位:毫秒)
long millisecond = endDate.getTime() - beginDate.getTime();
/**
* Math.abs((int)(millisecond/1000)); 绝对值 1秒 = 1000毫秒
* millisecond/1000 --> 秒 millisecond/1000*60 - > 分钟
* millisecond/(1000*60*60) -- > 小时 millisecond/(1000*60*60*24) -->
* 天
* */
switch (f) {
case 0: // second
return (millisecond / 1000);
case 1: // minute
return (millisecond / (1000 * 60));
case 2: // hour
return (millisecond / (1000 * 60 * 60));
case 3: // day
return (millisecond / (1000 * 60 * 60 * 24));
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 计算时间差
*
* @param starTime 开始时间
* @param endTime 结束时间
* @return 返回时间差
* @param返回类型==1----天,时,分。 ==2----时
*/
public String getTimeDifference(String starTime, String endTime) {
String timeString = "";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
Date parse = dateFormat.parse(starTime);
Date parse1 = dateFormat.parse(endTime);
long diff = parse1.getTime() - parse.getTime();
long day = diff / (24 * 60 * 60 * 1000);
long hour = (diff / (60 * 60 * 1000) - day * 24);
long min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
long ms = (diff - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000
- min * 60 * 1000 - s * 1000);
// System.out.println(day + "天" + hour + "小时" + min + "分" + s +
// "秒");
long hour1 = diff / (60 * 60 * 1000);
String hourString = hour1 + "";
long min1 = ((diff / (60 * 1000)) - hour1 * 60);
timeString = hour1 + "小时" + min1 + "分";
// System.out.println(day + "天" + hour + "小时" + min + "分" + s +
// "秒");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (java.text.ParseException e) {
e.printStackTrace();
}
return timeString;
}
/**
* Java YEAR、MONTH、DAY_OF_MONTH、HOUR加减法,int num +(日期前) -(日期后)
*
* @param num
* @param type
* @return
*/
public static String timeDateCompute(int num, int type) {
// YEAR、MONTH、DAY_OF_MONTH、HOUR 等
Calendar cal = Calendar.getInstance();//使用默认时区和语言环境获得一个日历。
if (type > 6) {
return null;
}
switch (type) {
case 0://年
cal.add(Calendar.YEAR, -num);
break;
case 1://月
cal.add(Calendar.MONTH, -num);
break;
case 2://日
cal.add(Calendar.DAY_OF_MONTH, -num);//取当前日期的前num天.
break;
case 3://时
cal.add(Calendar.HOUR_OF_DAY, -num);
break;
case 4://分
cal.add(Calendar.MINUTE, -num);
break;
case 5://秒
cal.add(Calendar.SECOND, -num);
break;
case 6://周
cal.add(Calendar.WEEK_OF_MONTH, -num);
break;
}
//通过格式化输出日期
SimpleDateFormat format = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
// SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (type == 0) {
System.out.println("Today is: " + format.format(Calendar.getInstance().getTime()));
}
System.out.println("CutNum is: " + format.format(cal.getTime()));
String CutNum = format.format(cal.getTime());
return CutNum;
}
/**
* 时间日期加减(-前,+后)
*
* @param statTime
* @param ymdhms
* @param type
* @return
*/
public String timeNum(Date statTime, int ymdhms, int type) {
String tn = null;
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
System.out.println("今天的日期:" + df.format(statTime));
System.out.println("两天前的日期:" + df.format(new Date(statTime.getTime() - 2 * 24 * 60 * 60 * 1000)));
System.out.println("三天后的日期:" + df.format(new Date(statTime.getTime() + 3 * 24 * 60 * 60 * 1000)));
switch (type) {
case 0://年
break;
case 1://月
break;
case 2://日
tn = df.format(new Date(statTime.getTime() - ymdhms * 24 * 60 * 60 * 1000));
break;
case 3://时
tn = df.format(new Date(statTime.getTime() - ymdhms * 60 * 60 * 1000));
break;
case 4://分
tn = df.format(new Date(statTime.getTime() - ymdhms * 60 * 1000));
break;
case 5://秒
tn = df.format(new Date(statTime.getTime() - ymdhms * 1000));
break;
}
return tn;
}
/**
* 时间日期加减(-前,+后)
*
* @param statTime
* @param year
* @param month
* @param day
* @param hour
* @param min
* @param sec
* @return
*/
public String timeNumStr(Date statTime, int year, int month, int day, int hour, int min, int sec) {
String tn = null;
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
System.out.println("今天的日期:" + df.format(statTime));
System.out.println("两天前的日期:" + df.format(new Date(statTime.getTime() - 2 * 24 * 60 * 60 * 1000)));
System.out.println("三天后的日期:" + df.format(new Date(statTime.getTime() + 3 * 24 * 60 * 60 * 1000)));
tn = df.format(new Date(statTime.getTime() - day * hour * min * sec * 1000));
return tn;
}
}