首页 > 代码库 > 开发中留意的一些Date操作,一般叫DateUtil类
开发中留意的一些Date操作,一般叫DateUtil类
package com.put.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * @ClassName: DateUtil * @Description: 时间帮助类 * @author Yegz * @date 2014-9-12 上午10:41:26 */ public class DateUtil { /** * @Title: getCurrTime * @Description: yyyy-MM-dd‘T‘HH:mm:ss * @throws ParseException * @return Date * @author Yegz * @date 2014-9-12 上午10:42:25 */ public static Date getCurrTime() throws ParseException{ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd‘T‘HH:mm:ss");//设置日期格式 String dateTime = df.format(new Date()); return df.parse(dateTime); } /** * @Title: getCurrTimeString * @Description: yyyy-MM-dd‘T‘HH:mm:ss * @throws ParseException * @return String * @author Yegz * @date 2014-9-12 上午10:41:43 */ public static String getCurrTimeString() throws ParseException{ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd‘T‘HH:mm:ss");//设置日期格式 return df.format(new Date()); } /** * @Title: getCurrTimeToNotT * @Description: yyyy-MM-dd HH:mm:ss * @throws ParseException * @return Date * @author Yegz * @date 2014-9-12 上午10:49:57 */ public static Date getCurrTimeToNotT() throws ParseException{ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 String dateTime = df.format(new Date()); return df.parse(dateTime); } /** * @Title: getCurrTimeStringToNotT * @Description: yyyy-MM-dd HH:mm:ss * @throws ParseException * @return String * @author Yegz * @date 2014-9-12 上午10:50:09 */ public static String getCurrTimeStringToNotT() throws ParseException{ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 return df.format(new Date()); } /** * @Title: getCurrTimeJustHMS * @Description: HH:mm:ss * @throws ParseException * @return Date * @author Yegz * @date 2014-9-12 上午10:54:30 */ public static Date getCurrTimeJustHMS() throws ParseException{ SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");//设置日期格式 String dateTime = df.format(new Date()); return df.parse(dateTime); } /** * @Title: getCurrTimeStringJustHMS * @Description: HH:mm:ss * @throws ParseException * @return String * @author Yegz * @date 2014-9-12 上午10:54:51 */ public static String getCurrTimeStringJustHMS() throws ParseException{ SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");//设置日期格式 return df.format(new Date()); } /** * @Title: getCurrTimeToMyFormat * @Description: 自定义格式,获取当前时间 * @param format * @throws ParseException * @return Date * @author Yegz * @date 2014-9-12 上午10:59:57 */ public static Date getCurrTimeToMyFormat(String format) throws ParseException{ SimpleDateFormat df = new SimpleDateFormat(format);//设置日期格式 String dateTime = df.format(new Date()); return df.parse(dateTime); } /** * @Title: getCurrTimeStringJustHMS * @Description: 自定义格式,获取当前时间 * @param format * @throws ParseException * @return String * @author Yegz * @date 2014-9-12 上午11:00:32 */ public static String getCurrTimeStringJustHMS(String format) throws ParseException{ SimpleDateFormat df = new SimpleDateFormat(format);//设置日期格式 return df.format(new Date()); } /** * @Title: getWeekShowInt * @Description: 显示传的时间星期几,数字显示 * @param date * @return int * @author Yegz * @date 2014-9-12 上午11:12:53 */ public static int getWeekShowInt(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal.get(Calendar.DAY_OF_WEEK)-1; } /** * @Title: getWeekShowString * @Description: 显示传的时间星期几,文字显示 * @param date * @return String * @author Yegz * @date 2014-9-12 上午11:13:55 */ public static String getWeekShowString(Date date) { Calendar cal = Calendar.getInstance(); String week = ""; cal.setTime(date); int today = cal.get(Calendar.DAY_OF_WEEK)-1; switch (today) { case 1: week = "星期一"; break; case 2: week = "星期二"; break; case 3: week = "星期三"; break; case 4: week = "星期四"; break; case 5: week = "星期五"; break; case 6: week = "星期六"; break; case 0: week = "星期天"; break; default: break; } return week; } /** * @Title: getTodayWeekShowInt * @Description: 获取今天的星期,数字显示 * @return int * @author Yegz * @date 2014-9-12 上午11:14:32 */ public static int getTodayWeekShowInt() { Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); return cal.get(Calendar.DAY_OF_WEEK)-1; } /** * @Title: getTodayWeekShowString * @Description: 获取今天的星期,文字显示 * @return String * @author Yegz * @date 2014-9-12 上午11:15:20 */ public static String getTodayWeekShowString() { Calendar cal = Calendar.getInstance(); String week = ""; cal.setTime(new Date()); int today = cal.get(Calendar.DAY_OF_WEEK)-1; switch (today) { case 1: week = "星期一"; break; case 2: week = "星期二"; break; case 3: week = "星期三"; break; case 4: week = "星期四"; break; case 5: week = "星期五"; break; case 6: week = "星期六"; break; case 0: week = "星期天"; break; default: break; } return week; } /** Test */ public static void main(String[] args) throws ParseException { // System.out.println(DateUtil.getCurrTimeStringJustHMS()); // System.out.println(DateUtil.getTodayWeekShowInt(new Date())); System.out.println(DateUtil.getTodayWeekShowString()); } }
本文出自 “9132951” 博客,请务必保留此出处http://9142951.blog.51cto.com/9132951/1551622
开发中留意的一些Date操作,一般叫DateUtil类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。