最近在程序开发中整理了很多日期格式化的函数,发布上来希望对大家有用
- /*********************************************************************
- * <p>Title: GetDate</p>
- * <p>Description: 常用日期格式化、日期处理函数</p>
- * <p>Copyright: Copyright (c) 2008</p>
- * <p>Company: ...</p>
- * @author rain
- * @version 1.0
- *********************************************************************/
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.GregorianCalendar;
- import com.neusoft.qd.sapinterface.sysconfig.SysConfig;
- public class GetDate {
- public GetDate() {
- }
- /**
- * 获取当前时间(日期+时间)
- * 返回时间格式是:yyyy-MM-dd HH:mm:ss
- * @return String
- */
- public static String getCurrentDate() {
- String dateString = "";
- Calendar calendar = null;
- SimpleDateFormat formatter = null;
- try {
- calendar = new GregorianCalendar();
- formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- dateString = formatter.format(calendar.getTime());
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return dateString;
- }
- /**
- * 获取当前时间(时分秒)
- * 返回时间格式可以为:
- * HH点mm分ss秒(11点10分15秒);
- * HHmmss(111015);
- * HH:mm:ss(11:10:15)
- * @return String
- */
- public static String getCurrentTime() {
- String dateString = "";
- Calendar calendar = null;
- SimpleDateFormat formatter = null;
- try {
- calendar = new GregorianCalendar();
- //打印时间格式为HHmmss,例如11点10分20秒的格式:111020
- //formatter = new SimpleDateFormat("HHmmss");
- //打印时间格式为HH:mm:ss,例如11点10分20秒的格式:11点10分20秒
- //formatter = new SimpleDateFormat("HH点mm分ss秒");
- //打印时间格式为HH:mm:ss,例如11点10分20秒的格式:11:10:20
- formatter = new SimpleDateFormat("HH:mm:ss");
- dateString = formatter.format(calendar.getTime());
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return dateString;
- }
- /**
- * 获取时间(yyyy-mm-dd格式)
- * 例如,2008-08-20
- * @return String
- */
- public static String getCurrentDay() {
- String dateString = "";
- Calendar calendar = null;
- SimpleDateFormat formatter = null;
- try {
- calendar = new GregorianCalendar();
- formatter = new SimpleDateFormat("yyyy-MM-dd");
- dateString = formatter.format(calendar.getTime());
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return dateString;
- }
- /**
- * 获取时间,格式是(yyyy-mm-dd)
- * 获取当前时间,减去从sysconfig.getDatebegin()获取的天数
- * 例如今天是2008-08-20,sysconfig.getDatebegin()的值是7,
- * 那么返回值为2008-08-13,以此类推
- * @return String
- */
- public static String getCurrentDaySubtract() {
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
- Calendar now = Calendar.getInstance();
- try {
- SysConfig sysconfig = new SysConfig();//读取指定文件中字段信息
- now.add(Calendar.DAY_OF_WEEK, Integer.parseInt(sysconfig.getDatebegin()));
- } catch (Exception e) {
- e.printStackTrace();
- }
- return df.format(now.getTime());
- }
- /**
- * 获取当前时间,时间格式(yyyymmdd)
- * 例如,20080820
- * @return String
- */
- public static String getCurrentDayYmd() {
- String dateString = "";
- Calendar calendar = null;
- SimpleDateFormat formatter = null;
- try {
- calendar = new GregorianCalendar();
- formatter = new SimpleDateFormat("yyyyMMdd");
- dateString = formatter.format(calendar.getTime());
- } catch (Exception ex) {
- }
- return dateString;
- }
- /**
- * 获取时间(yyyymmdd格式)
- * 获取两天前的时间
- * @return String
- */
- public static String getLast2Ymd() {
- String dateString = "";
- Calendar calendar = null;
- SimpleDateFormat formatter = null;
- try {
- calendar = new GregorianCalendar();
- formatter = new SimpleDateFormat("yyyyMMdd");
- calendar.set(java.util.Calendar.DAY_OF_MONTH, calendar
- .get(java.util.Calendar.DAY_OF_MONTH) - 2);
- dateString = formatter.format(calendar.getTime());
- } catch (Exception ex) {
- }
- return dateString;
- }
- /**
- * 获取时间(yyyy-mm-dd格式)
- * 获取当前日期加一后的日期,即明天的日期
- * @return String
- */
- public static String getTom() {
- String dateString = "";
- try {
- java.util.Date pre_date = new java.util.Date(System.currentTimeMillis()
- + 1 * 24 * 3600 * 1000);
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
- dateString = formatter.format(pre_date);
- } catch (Exception ex) {
- }
- return dateString;
- }
- /**
- * 获取指定周周一对应的日期,日期格式(yyyy-MM-dd)
- * 输入参数int iNext对应的值是几,那么就计算几个周之前或之后的周一对应的日期
- * 参数int iNext可为正值或负值
- * 例如int iNext,值为1时,计算的是下个周周一对应的日期时间,
- * int iNext,值为-1时,计算的是上个周周一对应的日期时间,
- * @return String
- */
- public static String getNextMonday(int iNext, String dateFormat) {
- String strRet = "";
- Date dt = new Date();
- GregorianCalendar gc = new GregorianCalendar();
- gc.setTime(dt);
- gc.set(7, 2);
- gc.add(5, 7 * iNext);
- if (dateFormat != null && !dateFormat.equals("")) {
- SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
- strRet = formatter.format(gc.getTime());
- } else {
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
- strRet = formatter.format(gc.getTime());
- }
- return strRet;
- }
- /**
- * 获取指定周周日对应的日期,日期格式(yyyy-MM-dd)
- * 输入参数int iNext对应的值是几,那么就计算几个周之前或之后的周日对应的日期
- * 参数int iNext可为正值或负值
- * 例如int iNext,值为1时,计算的是下个周周日对应的日期时间,
- * int iNext,值为-1时,计算的是上个周周日对应的日期时间,
- * @return String
- */
- public static String getNextSunday(int iNext, String dateFormat) {
- String strRet = "";
- Date dt = new Date();
- GregorianCalendar gc = new GregorianCalendar();
- gc.setTime(dt);
- gc.set(7, 1);
- gc.add(5, 7 * iNext);
- if (dateFormat != null && !dateFormat.equals("")) {
- SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
- strRet = formatter.format(gc.getTime());
- }
- else {
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
- strRet = formatter.format(gc.getTime());
- }
- return strRet;
- }
- public static void main(String[] agrv) {
- System.out.println("函数getCurrentDate()打印的日期格式 :"
- + GetDate.getCurrentDate());
- System.out.println("函数getCurrentTime()打印的日期格式 :"
- + GetDate.getCurrentTime());
- System.out.println("函数getCurrentDay()打印的日期格式 :"
- + GetDate.getCurrentDay());
- System.out.println("函数getCurrentDaySubtract打印的日期格式 :"
- + GetDate.getCurrentDaySubtract());
- System.out.println("函数getCurrentDayYmd()打印的日期格式 :"
- + GetDate.getCurrentDayYmd());
- System.out.println("函数getLast2Ymd()打印的日期格式 :"
- + GetDate.getLast2Ymd());
- System.out.println("函数getTom()打印的日期格式 :" + GetDate.getTom());
- System.out.println("函数getNextMonday()打印的日期格式 :"
- + GetDate.getNextMonday(1, ""));
- System.out.println("函数getNextSunday()打印的日期格式 :"
- + GetDate.getNextSunday(1, ""));
- }
- }
2008年08月21日运行main函数后打印结果如下:
函数getCurrentDate()打印的日期格式 :2008-08-21 11:32:59
函数getCurrentTime()打印的日期格式 :11:32:59
函数getCurrentDay()打印的日期格式 :2008-08-21
函数getCurrentDaySubtract打印的日期格式 :2008-08-14
函数getCurrentDayYmd()打印的日期格式 :20080821
函数getLast2Ymd()打印的日期格式 :20080819
函数getTom()打印的日期格式 :2008-08-22
函数getNextMonday()打印的日期格式 :2008-08-25
函数getNextSunday()打印的日期格式 :2008-08-24
过几天将发布整理后的常用工具类,例如:编码格式转换、分割、替换子串、追加等功能。