Android关于时间日期相关常用方法
2017-06-26 17:29
477 查看
public class DateUtils {
/**
* 获取当前时间的 毫秒数
* 参数格式:yyyy-MM-dd HH:mm:ss
* @param time
* @return
* @throws ParseException
*/
public static long getTimeInMillis(String time) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(time));
long timeInMillis = cal.getTimeInMillis();
return timeInMillis;
}
/**
* 获取当前日期 格式: yyyy-mm-dd HH:mm :ss (年-月-日 时:分:秒)
*/
public static String getDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(new Date());
}
/**
*
* 得到当前系统日期 格式: yyyy-MM-dd (年-月-日)
*/
public static String getDate1() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(new Date());
}
/**
* 得到当前系统时间
*
* @return 格式为yyyyMMdd 20120301
*/
public static String getDate2() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.format(date);// 系统的当前时间
}
/**
* 得到当前系统的 HH:mm:ss
*/
public static String getTime() {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
return sdf.format(new Date());
}
/**
* 得到系统当前时间
* @return 格式为yyyyMMddHHmm
*/
public static String getTime2() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
return sdf.format(new Date());
}
/*
* 获取当前日期是 星期几
* **/
public static String getWeek() {
String currentWeek = null;
Calendar c = Calendar.getInstance();
String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
if (mWay.equals("1")) {
currentWeek = "星期天";
}else if (mWay.equals("2")) {
currentWeek = "星期一";
}else if (mWay.equals("3")) {
currentWeek = "星期二";
}else if (mWay.equals("4")) {
currentWeek = "星期三";
}else if (mWay.equals("5")) {
currentWeek = "星期四";
}else if (mWay.equals("6")) {
currentWeek = "星期五";
}else if (mWay.equals("7")) {
currentWeek = "星期六";
}
return currentWeek;
}
/**
* 计算两个日期之间相差的天数
*
* @param smdate
* 较小的时间
* @param bdate
* 较大的时间
* @return 相差天数
* @throws ParseException
*/
public static int daysBetween(Date smdate, Date bdate)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
smdate = sdf.parse(sdf.format(smdate));
bdate = sdf.
a1cf
parse(sdf.format(bdate));
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 字符串的日期格式的计算
* 时间格式:yyyyMMdd
* @param smdate
* 较小的时间
* @param bdate
* 较大的时间
* @return 相差天数
*/
public static int daysBetweenString(String smdate, String bdate)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
// System.out.println(between_days+"-------------");
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 字符串的日期格式的计算
* 时间格式:yyyyMMddHHmmss
* @param smdate
* 较小的时间
* @param bdate
* 较大的时间
* @return 相差天数
*/
public static int daysBetween(String smdate, String bdate)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
// System.out.println(between_days+"-------------");
return (int) between_days;
}
/**
* 字符串的日期格式的计算
* 时间格式:yyyyMMddHHmmss
* @param currentTime 当前的时间
* @param beginTime 开始的时间
* @param endTime 结束的时间
* @return boolean 当前日期是否在给定开始和结束日期之间
*/
public static boolean isCurrentTimeBetween(String currentTime,String beginTime,String endTime) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(currentTime));
long currentTimeTimeInMillis = cal.getTimeInMillis();
cal.setTime(sdf.parse(beginTime));
long beginTimeInMillis = cal.getTimeInMillis();
cal.setTime(sdf.parse(endTime));
long endTimeInMillis = cal.getTimeInMillis();
if (currentTimeTimeInMillis > beginTimeInMillis && currentTimeTimeInMillis < endTimeInMillis) {
return true;
}
return false;
}
}
/**
* 获取当前时间的 毫秒数
* 参数格式:yyyy-MM-dd HH:mm:ss
* @param time
* @return
* @throws ParseException
*/
public static long getTimeInMillis(String time) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(time));
long timeInMillis = cal.getTimeInMillis();
return timeInMillis;
}
/**
* 获取当前日期 格式: yyyy-mm-dd HH:mm :ss (年-月-日 时:分:秒)
*/
public static String getDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(new Date());
}
/**
*
* 得到当前系统日期 格式: yyyy-MM-dd (年-月-日)
*/
public static String getDate1() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(new Date());
}
/**
* 得到当前系统时间
*
* @return 格式为yyyyMMdd 20120301
*/
public static String getDate2() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.format(date);// 系统的当前时间
}
/**
* 得到当前系统的 HH:mm:ss
*/
public static String getTime() {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
return sdf.format(new Date());
}
/**
* 得到系统当前时间
* @return 格式为yyyyMMddHHmm
*/
public static String getTime2() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
return sdf.format(new Date());
}
/*
* 获取当前日期是 星期几
* **/
public static String getWeek() {
String currentWeek = null;
Calendar c = Calendar.getInstance();
String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
if (mWay.equals("1")) {
currentWeek = "星期天";
}else if (mWay.equals("2")) {
currentWeek = "星期一";
}else if (mWay.equals("3")) {
currentWeek = "星期二";
}else if (mWay.equals("4")) {
currentWeek = "星期三";
}else if (mWay.equals("5")) {
currentWeek = "星期四";
}else if (mWay.equals("6")) {
currentWeek = "星期五";
}else if (mWay.equals("7")) {
currentWeek = "星期六";
}
return currentWeek;
}
/**
* 计算两个日期之间相差的天数
*
* @param smdate
* 较小的时间
* @param bdate
* 较大的时间
* @return 相差天数
* @throws ParseException
*/
public static int daysBetween(Date smdate, Date bdate)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
smdate = sdf.parse(sdf.format(smdate));
bdate = sdf.
a1cf
parse(sdf.format(bdate));
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 字符串的日期格式的计算
* 时间格式:yyyyMMdd
* @param smdate
* 较小的时间
* @param bdate
* 较大的时间
* @return 相差天数
*/
public static int daysBetweenString(String smdate, String bdate)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
// System.out.println(between_days+"-------------");
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 字符串的日期格式的计算
* 时间格式:yyyyMMddHHmmss
* @param smdate
* 较小的时间
* @param bdate
* 较大的时间
* @return 相差天数
*/
public static int daysBetween(String smdate, String bdate)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
// System.out.println(between_days+"-------------");
return (int) between_days;
}
/**
* 字符串的日期格式的计算
* 时间格式:yyyyMMddHHmmss
* @param currentTime 当前的时间
* @param beginTime 开始的时间
* @param endTime 结束的时间
* @return boolean 当前日期是否在给定开始和结束日期之间
*/
public static boolean isCurrentTimeBetween(String currentTime,String beginTime,String endTime) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(currentTime));
long currentTimeTimeInMillis = cal.getTimeInMillis();
cal.setTime(sdf.parse(beginTime));
long beginTimeInMillis = cal.getTimeInMillis();
cal.setTime(sdf.parse(endTime));
long endTimeInMillis = cal.getTimeInMillis();
if (currentTimeTimeInMillis > beginTimeInMillis && currentTimeTimeInMillis < endTimeInMillis) {
return true;
}
return false;
}
}
相关文章推荐
- Android平台关于时间和日期的相关类和方法(Date/Time)
- Android平台关于时间和日期的相关类和方法(Date/Time)
- Android平台关于时间和日期的相关类和方法(Date/Time)
- android内部培训视频_第三节(3)_常用控件(ViewPager、日期时间相关、ListView)
- MYSQL中常见时间日期相关的表示方法与常用相关函数
- Android随笔之——Android时间、日期相关类和方法
- JAVA中关于时间跟日期的一些常用方法
- java日期的往前推几天,关于时间的常用方法
- java中关于时间日期操作的常用函数
- 003 - js中Date日期时间常用方法
- Android编程计算函数时间戳的相关方法总结
- Android 常用控件(RadioButton、CheckBox)、.9图、Toast(吐司面包,提示)、时间日期对话框 二
- JAVA处理日期时间常用方法: java.util.Calendar
- 关于XStream相关的常用方法;设置别名;忽略属性;设置xml字段属性
- JAVA处理日期时间常用方法
- java中关于时间日期操作的常用函数
- 常用的日期时间处理方法
- Python常用的日期时间处理方法示例
- Android中关于日期时间与时区的使用总结