时间格式转变 android,Android开发中日期工具类DateUtil完整实例

本文实例讲述了Android开发中日期工具类DateUtil。分享给大家供大家参考,具体如下:

/**

* 日期操作工具类.

* @Project ERPForAndroid

* @Package com.ymerp.android.tools

* @author chenlin

* @version 1.0

*/

@SuppressLint("SimpleDateFormat")

public class DateUtil {

private static final String FORMAT = "yyyy-MM-dd HH:mm:ss";

private static final SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

private static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

public static Date str2Date(String str) {

return str2Date(str, null);

}

/**

* 字符串转时间

* @param str

* @param format

* @return

*/

public static Date str2Date(String str, String format) {

if (str == null || str.length() == 0) {

return null;

}

if (format == null || format.length() == 0) {

format = FORMAT;

}

Date date = null;

try {

SimpleDateFormat sdf = new SimpleDateFormat(format);

date = sdf.parse(str);

} catch (Exception e) {

e.printStackTrace();

}

return date;

}

public static Calendar str2Calendar(String str) {

return str2Calendar(str, null);

}

public static Calendar str2Calendar(String str, String format) {

Date date = str2Date(str, format);

if (date == null) {

return null;

}

Calendar c = Calendar.getInstance();

c.setTime(date);

return c;

}

public static String date2Str(Calendar c) {// yyyy-MM-dd HH:mm:ss

return date2Str(c, null);

}

public static String date2Str(Calendar c, String format) {

if (c == null) {

return null;

}

return date2Str(c.getTime(), format);

}

public static String date2Str(Date d) {// yyyy-MM-dd HH:mm:ss

return date2Str(d, null);

}

public static String date2Str(Date d, String format) {// yyyy-MM-dd HH:mm:ss

if (d == null) {

return null;

}

if (format == null || format.length() == 0) {

format = FORMAT;

}

SimpleDateFormat sdf = new SimpleDateFormat(format);

String s = sdf.format(d);

return s;

}

public static String getCurDateStr() {

Calendar c = Calendar.getInstance();

c.setTime(new Date());

return c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DAY_OF_MONTH) + "-"

+ c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND);

}

/**

* 获得当前日期的字符串格式

*

* @param format

* @return

*/

public static String getCurDateStr(String format) {

Calendar c = Calendar.getInstance();

return date2Str(c, format);

}

/**

* 格式到秒

*

* @param time

* @return

*/

public static String getMillon(long time) {

return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(time);

}

/**

* 格式到天

*

* @param time

* @return

*/

public static String getDay(long time) {

return new SimpleDateFormat("yyyy-MM-dd").format(time);

}

/**

* 格式到毫秒

*

* @param time

* @return

*/

public static String getSMillon(long time) {

return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(time);

}

/**

* 字符串转换到时间格式

*

* @param dateStr

* 需要转换的字符串

* @param formatStr

* 需要格式的目标字符串 举例 yyyy-MM-dd

* @return Date 返回转换后的时间

* @throws ParseException

* 转换异常

*/

public static Date StringToDate(String dateStr, String formatStr) {

DateFormat sdf = new SimpleDateFormat(formatStr);

Date date = null;

try {

date = sdf.parse(dateStr);

} catch (ParseException e) {

e.printStackTrace();

}

return date;

}

/**

* 转化时间输入时间与当前时间的间隔

*

* @param timestamp

* @return

*/

public static String converTime(long timestamp) {

long currentSeconds = System.currentTimeMillis() / 1000;

long timeGap = currentSeconds - timestamp;// 与现在时间相差秒数

String timeStr = null;

if (timeGap > 24 * 60 * 60) {// 1天以上

timeStr = timeGap / (24 * 60 * 60) + "天前";

} else if (timeGap > 60 * 60) {// 1小时-24小时

timeStr = timeGap / (60 * 60) + "小时前";

} else if (timeGap > 60) {// 1分钟-59分钟

timeStr = timeGap / 60 + "分钟前";

} else {// 1秒钟-59秒钟

timeStr = "刚刚";

}

return timeStr;

}

/**

* 把字符串转化为时间格式

*

* @param timestamp

* @return

*/

public static String getStandardTime(long timestamp) {

SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 HH:mm");

Date date = new Date(timestamp * 1000);

sdf.format(date);

return sdf.format(date);

}

/**

* 获得当前日期时间 日期时间格式yyyy-MM-dd HH:mm:ss

*

* @return

*/

public static String currentDatetime() {

return datetimeFormat.format(now());

}

/**

* 格式化日期时间 日期时间格式yyyy-MM-dd HH:mm:ss

*

* @return

*/

public static String formatDatetime(Date date) {

return datetimeFormat.format(date);

}

/**

* 获得当前时间 时间格式HH:mm:ss

*

* @return

*/

public static String currentTime() {

return timeFormat.format(now());

}

/**

* 格式化时间 时间格式HH:mm:ss

*

* @return

*/

public static String formatTime(Date date) {

return timeFormat.format(date);

}

/**

* 获得当前时间的java.util.Date对象

*

* @return

*/

public static Date now() {

return new Date();

}

public static Calendar calendar() {

Calendar cal = GregorianCalendar.getInstance(Locale.CHINESE);

cal.setFirstDayOfWeek(Calendar.MONDAY);

return cal;

}

/**

* 获得当前时间的毫秒数

*

* 详见{@link System#currentTimeMillis()}

*

* @return

*/

public static long millis() {

return System.currentTimeMillis();

}

/**

*

* 获得当前Chinese月份

*

* @return

*/

public static int month() {

return calendar().get(Calendar.MONTH) + 1;

}

/**

* 获得月份中的第几天

*

* @return

*/

public static int dayOfMonth() {

return calendar().get(Calendar.DAY_OF_MONTH);

}

/**

* 今天是星期的第几天

*

* @return

*/

public static int dayOfWeek() {

return calendar().get(Calendar.DAY_OF_WEEK);

}

/**

* 今天是年中的第几天

*

* @return

*/

public static int dayOfYear() {

return calendar().get(Calendar.DAY_OF_YEAR);

}

/**

* 判断原日期是否在目标日期之前

*

* @param src

* @param dst

* @return

*/

public static boolean isBefore(Date src, Date dst) {

return src.before(dst);

}

/**

* 判断原日期是否在目标日期之后

*

* @param src

* @param dst

* @return

*/

public static boolean isAfter(Date src, Date dst) {

return src.after(dst);

}

/**

* 判断两日期是否相同

*

* @param date1

* @param date2

* @return

*/

public static boolean isEqual(Date date1, Date date2) {

return date1.compareTo(date2) == 0;

}

/**

* 判断某个日期是否在某个日期范围

*

* @param beginDate

* 日期范围开始

* @param endDate

* 日期范围结束

* @param src

* 需要判断的日期

* @return

*/

public static boolean between(Date beginDate, Date endDate, Date src) {

return beginDate.before(src) && endDate.after(src);

}

/**

* 获得当前月的最后一天

*

* HH:mm:ss为0,毫秒为999

*

* @return

*/

public static Date lastDayOfMonth() {

Calendar cal = calendar();

cal.set(Calendar.DAY_OF_MONTH, 0); // M月置零

cal.set(Calendar.HOUR_OF_DAY, 0);// H置零

cal.set(Calendar.MINUTE, 0);// m置零

cal.set(Calendar.SECOND, 0);// s置零

cal.set(Calendar.MILLISECOND, 0);// S置零

cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);// 月份+1

cal.set(Calendar.MILLISECOND, -1);// 毫秒-1

return cal.getTime();

}

/**

* 获得当前月的第一天

*

* HH:mm:ss SS为零

*

* @return

*/

public static Date firstDayOfMonth() {

Calendar cal = calendar();

cal.set(Calendar.DAY_OF_MONTH, 1); // M月置1

cal.set(Calendar.HOUR_OF_DAY, 0);// H置零

cal.set(Calendar.MINUTE, 0);// m置零

cal.set(Calendar.SECOND, 0);// s置零

cal.set(Calendar.MILLISECOND, 0);// S置零

return cal.getTime();

}

private static Date weekDay(int week) {

Calendar cal = calendar();

cal.set(Calendar.DAY_OF_WEEK, week);

return cal.getTime();

}

/**

* 获得周五日期

*

* 注:日历工厂方法{@link #calendar()}设置类每个星期的第一天为Monday,US等每星期第一天为sunday

*

* @return

*/

public static Date friday() {

return weekDay(Calendar.FRIDAY);

}

/**

* 获得周六日期

*

* 注:日历工厂方法{@link #calendar()}设置类每个星期的第一天为Monday,US等每星期第一天为sunday

*

* @return

*/

public static Date saturday() {

return weekDay(Calendar.SATURDAY);

}

/**

* 获得周日日期 注:日历工厂方法{@link #calendar()}设置类每个星期的第一天为Monday,US等每星期第一天为sunday

*

* @return

*/

public static Date sunday() {

return weekDay(Calendar.SUNDAY);

}

/**

* 将字符串日期时间转换成java.util.Date类型 日期时间格式yyyy-MM-dd HH:mm:ss

*

* @param datetime

* @return

*/

public static Date parseDatetime(String datetime) throws ParseException {

return datetimeFormat.parse(datetime);

}

/**

* 将字符串日期转换成java.util.Date类型 日期时间格式yyyy-MM-dd

*

* @param date

* @return

* @throws ParseException

*/

public static Date parseDate(String date) throws ParseException {

return dateFormat.parse(date);

}

/**

* 将字符串日期转换成java.util.Date类型 时间格式 HH:mm:ss

*

* @param time

* @return

* @throws ParseException

*/

public static Date parseTime(String time) throws ParseException {

return timeFormat.parse(time);

}

/**

* 根据自定义pattern将字符串日期转换成java.util.Date类型

*

* @param datetime

* @param pattern

* @return

* @throws ParseException

*/

public static Date parseDatetime(String datetime, String pattern) throws ParseException {

SimpleDateFormat format = (SimpleDateFormat) datetimeFormat.clone();

format.applyPattern(pattern);

return format.parse(datetime);

}

/**

* 把秒格式化为分种小时

*

* @param second

* @return

*/

public static String parseSecond(int second) {

if (second >= 60) {

return second / 60 + "分";

} else if (second >= 60 * 60) {

return second / 60 * 60 + "时";

} else if (second >= 60 * 60 * 24) {

return second / 60 * 60 + "天";

} else {

return second + "秒";

}

}

/**

* 比较时间大小

* @param begin

* @param end

* @return

*/

public static int compareDate(String begin, String end) {

DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");

try {

Date beginDate = df.parse(begin);

Date endDate = df.parse(end);

if (beginDate.getTime() < endDate.getTime()) {

return 1;

} else if (beginDate.getTime() > endDate.getTime()) {

return -1;

} else {

return 0;

}

} catch (Exception exception) {

exception.printStackTrace();

}

return 0;

}

/**

* 获得年份

* @param date

* @return

*/

public int getYear(Date date){

Calendar c = Calendar.getInstance();

c.setTime(date);

return c.get(Calendar.YEAR);

}

/**

* 获得月份

* @param date

* @return

*/

public int getMonth(Date date){

Calendar c = Calendar.getInstance();

c.setTime(date);

return c.get(Calendar.MONTH) + 1;

}

/**

* 获得星期几

* @param date

* @return

*/

public int getWeek(Date date){

Calendar c = Calendar.getInstance();

c.setTime(date);

return c.get(Calendar.WEEK_OF_YEAR);

}

/**

* 获得日期

* @param date

* @return

*/

public int getDay(Date date){

Calendar c = Calendar.getInstance();

c.setTime(date);

return c.get(Calendar.DATE);

}

/**

* 获得天数差

* @param begin

* @param end

* @return

*/

public long getDayDiff(Date begin, Date end){

long day = 1;

if(end.getTime() < begin.getTime()){

day = -1;

}else if(end.getTime() == begin.getTime()){

day = 1;

}else {

day += (end.getTime() - begin.getTime())/(24 * 60 * 60 * 1000) ;

}

return day;

}

}

PS:这里再为大家推荐几款关于日期与时间计算的在线工具供大家参考使用:

希望本文所述对大家Android程序设计有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值