失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Date DateTime String等日期格式的相互转化

Date DateTime String等日期格式的相互转化

时间:2022-12-20 19:13:27

相关推荐

Date DateTime String等日期格式的相互转化

日期之间的转化关系:

1,获取当前系统时间

Date date = new Date();

2,Date转为DateTime

DateTime dateTime = new DateTime(date.getTime());

3,DateTime转为Date

Date date = dateTime.toDate();

4,获取日期格式,

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

5,Date转换为String类型格式

String dateStr = df.format(new Date());

工具类

/*** 返回当前日期时间字符串<br>* 默认格式:yyyy-mm-dd hh:mm:ss** @return String 返回当前字符串型日期时间*/public static String getCurrentTime() {String returnStr = null;SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date();returnStr = f.format(date);return returnStr;}/*** 返回当前日期时间字符串<br>* 默认格式:yyyymmddhhmmss** @return String 返回当前字符串型日期时间*/public static BigDecimal getCurrentTimeAsNumber() {String returnStr = null;SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");Date date = new Date();returnStr = f.format(date);return new BigDecimal(returnStr);}/*** 返回自定义格式的当前日期时间字符串** @param format* 格式规则* @return String 返回当前字符串型日期时间*/public static String getCurrentTime(String format) {String returnStr = null;SimpleDateFormat f = new SimpleDateFormat(format);Date date = new Date();returnStr = f.format(date);return returnStr;}/*** 返回当前字符串型日期** @return String 返回的字符串型日期*/public static String getCurDate() {Calendar calendar = Calendar.getInstance();SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd");String strDate = simpledateformat.format(calendar.getTime());return strDate;}/*** 返回指定格式的字符型日期* @param date* @param formatString* @return*/public static String Date2String(Date date, String formatString) {if (G4Utils.isEmpty(date)) {return null;}SimpleDateFormat simpledateformat = new SimpleDateFormat(formatString);String strDate = simpledateformat.format(date);return strDate;}/*** 返回当前字符串型日期** @param format* 格式规则** @return String 返回的字符串型日期*/public static String getCurDate(String format) {Calendar calendar = Calendar.getInstance();SimpleDateFormat simpledateformat = new SimpleDateFormat(format);String strDate = simpledateformat.format(calendar.getTime());return strDate;}/*** 返回TimeStamp对象** @return*/public static Timestamp getCurrentTimestamp() {Object obj = TypeCaseHelper.convert(getCurrentTime(), "Timestamp", "yyyy-MM-dd HH:mm:ss");if (obj != null)return (Timestamp) obj;elsereturn null;}/*** 将字符串型日期转换为日期型** @param strDate* 字符串型日期* @param srcDateFormat* 源日期格式* @param dstDateFormat* 目标日期格式* @return Date 返回的util.Date型日期*/public static Date stringToDate(String strDate, String srcDateFormat, String dstDateFormat) {Date rtDate = null;Date tmpDate = (new SimpleDateFormat(srcDateFormat)).parse(strDate, new ParsePosition(0));String tmpString = null;if (tmpDate != null)tmpString = (new SimpleDateFormat(dstDateFormat)).format(tmpDate);if (tmpString != null)rtDate = (new SimpleDateFormat(dstDateFormat)).parse(tmpString, new ParsePosition(0));return rtDate;}/*** dp转px** @param context* @param* @return*/public static float dp2px(Context context, int dpVal) {return TypedValue.applyDimension(PLEX_UNIT_DIP,dpVal, context.getResources().getDisplayMetrics());}/*** sp转px** @param context* @param spVal* @return*/public static float sp2px(Context context, float spVal) {return TypedValue.applyDimension(PLEX_UNIT_SP,spVal, context.getResources().getDisplayMetrics());}/*** 屏幕宽度** @param context* @return*/public static int getDisplayWidth(Context context) {return context.getResources().getDisplayMetrics().widthPixels;}/*** 屏幕高度** @param context* @return*/public static int getDisplayHeight(Context context) {return context.getResources().getDisplayMetrics().heightPixels;}//是否同月public static boolean isEqualsMonth(DateTime dateTime1, DateTime dateTime2) {return dateTime1.getMonthOfYear() == dateTime2.getMonthOfYear();}/*** 第一个是不是第二个的上一个月,只在此处有效** @param dateTime1* @param dateTime2* @return*/public static boolean isLastMonth(DateTime dateTime1, DateTime dateTime2) {DateTime dateTime = dateTime2.plusMonths(-1);return dateTime1.getMonthOfYear() == dateTime.getMonthOfYear();}/*** 第一个是不是第二个的下一个月,只在此处有效* @param dateTime1* @param dateTime2* @return*/public static boolean isNextMonth(DateTime dateTime1, DateTime dateTime2) {DateTime dateTime = dateTime2.plusMonths(1);return dateTime1.getMonthOfYear() == dateTime.getMonthOfYear();}/*** 获得两个日期距离几个月* @return*/public static int getIntervalMonths(DateTime dateTime1, DateTime dateTime2) {return (dateTime2.getYear() - dateTime1.getYear()) * 12 + (dateTime2.getMonthOfYear() - dateTime1.getMonthOfYear());}/*** 获得两个日期距离几周** @param dateTime1* @param dateTime2* @return*/public static int getIntervalWeek(DateTime dateTime1, DateTime dateTime2) {DateTime sunFirstDayOfWeek1 = getSunFirstDayOfWeek(dateTime1);DateTime sunFirstDayOfWeek2 = getSunFirstDayOfWeek(dateTime2);int days = Days.daysBetween(sunFirstDayOfWeek1, sunFirstDayOfWeek2).getDays();if (days > 0) {return (days + 1) / 7;} else if (days < 0) {return (days - 1) / 7;} else {return days;}}/*** 是否是今天* @param dateTime* @return*/public static boolean isToday(DateTime dateTime) {return new DateTime().toLocalDate().equals(dateTime.toLocalDate());}/*** 某月第一天是周几** @return*/public static int getFirstDayOfWeekOfMonth(int year, int month) {int dayOfWeek = new DateTime(year, month, 1, 0, 0, 0).getDayOfWeek();if (dayOfWeek == 7) {return 0;}return dayOfWeek;}//转化一周从周日开始public static DateTime getSunFirstDayOfWeek(DateTime dateTime) {if (dateTime.dayOfWeek().get() == 7) {return dateTime;} else {return dateTime.minusWeeks(1).withDayOfWeek(7);}}/*** 通过年份和月份 得到当月的日子** @param year* @param month* @return*/public static int getMonthDays(int year, int month) {month++;switch (month) {case 1:case 3:case 5:case 7:case 8:case 10:case 12:return 31;case 4:case 6:case 9:case 11:return 30;case 2:if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {return 29;} else {return 28;}default:return -1;}}/*** 获取这个月有几行** @param year* @param month* @return*/public static int getMonthRows(int year, int month) {int size = getFirstDayOfWeekOfMonth(year, month) + getMonthDays(year, month) - 1;return size % 7 == 0 ? size / 7 : (size / 7) + 1;}/*** 返回当前月份1号位于周几** @param year 年份* @param month 月份,传入系统获取的,不需要正常的* @return 日:1 一:2 二:3 三:4 四:5 五:6 六:7*/public static int getFirstDayWeek(int year, int month) {Calendar calendar = Calendar.getInstance();calendar.set(year, month, 1);return calendar.get(Calendar.DAY_OF_WEEK);}public static int getYearByTimeStamp(long timeStamp) {String date = timeStampToDate(timeStamp);String year = date.substring(0, 4);return Integer.parseInt(year);}public static String timeStampToDate(long timeStamp) {Date date = new Date(timeStamp);SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String dateStr = simpleDateFormat.format(date);return dateStr;}/*** 获取当前日期是星期几<br>** @param dt* @return 当前日期是星期几*/public static Integer getWeekOfDate(DateTime dt) {// String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};Integer[] weekDays = {0, 1, 2, 3, 4, 5, 6};Calendar cal = Calendar.getInstance();cal.setTime(dt.toDate());int w = cal.get(Calendar.DAY_OF_WEEK) - 1;if (w < 0)w = 0;return weekDays[w];}/*** 获取一个日期加上天数得到最后的日期** @param d 日期* @param day 加的天数 如果天数是负数,即是减* @return 当前日期是星期几*/public static Date addDate(Date d, long day) {long time = d.getTime();day = day * 24 * 60 * 60 * 1000;time += day;return new Date(time);}

如果觉得《Date DateTime String等日期格式的相互转化》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。