datautil java_Java 获取固定格式的日期工具类 DateUtil

package com.util;      import java.text.ParseException;   import java.text.ParsePosition;   import java.text.SimpleDateFormat;   import java.util.Cale

/*

* Created on 2013.04.17

*

* TODO To change the template for this generated file go to

* Window - Preferences - Java - Code Style - Code Templates

*/

package com.util;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

/**

* @author Hua

*

* TODO To change the template for this generated type comment go to Window -

* Preferences - Java - Code Style - Code Templates

*/

public class DateUtil {

public static String getNowDateAndTime() {

SimpleDateFormat dateformat = new SimpleDateFormat(

"yyyy-MM-dd HH:mm:ss");

return dateformat.format(new Date());

}

public static String getNowTime() {

SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd H:mm:ss");

return dateformat.format(new Date());

}

public static String getNowHMS() {

SimpleDateFormat dateformat = new SimpleDateFormat("HH:mm:ss");

return dateformat.format(new Date());

}

public static String getNowYear() {

SimpleDateFormat dateformat = new SimpleDateFormat("yyyy");

return dateformat.format(new Date());

}

public static String getNowMonth() {

SimpleDateFormat dateformat = new SimpleDateFormat("MM");

return dateformat.format(new Date());

}

public static String getNowDay() {

SimpleDateFormat dateformat = new SimpleDateFormat("dd");

return dateformat.format(new Date());

}

public static String getNowDate() {

SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");

return dateformat.format(new Date());

}

public static String getBuinessDate() {

SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");

return dateformat.format(new Date());

}

public static String getBuinessTime() {

SimpleDateFormat dateformat = new SimpleDateFormat("H");

return dateformat.format(new Date());

}

public static String getBuinessTimeExact() {

SimpleDateFormat dateformat = new SimpleDateFormat("H:mm");

return dateformat.format(new Date());

}

public static String getTomrrowDate() {

SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");

Date tomrrow = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);

return dateformat.format(tomrrow);

}

public static String getYesterdayDate(String type, int num) {

Date tomrrow = null;

SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");

if (type.equals(("add"))) {

tomrrow = new Date(new Date().getTime() + 24 * 60 * 60 * 1000 * num);

} else if (type.equals(("sub"))) {

tomrrow = new Date(new Date().getTime() - 24 * 60 * 60 * 1000 * num);

}

return dateformat.format(tomrrow);

}

public static String getYesterdayDate(String date, String type, int num) {

Date tomrrow = null;

SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");

try {

Date temp = dateformat.parse(date);

if (type.equals(("add"))) {

tomrrow = new Date(temp.getTime() + 24 * 60 * 60 * 1000 * num);

} else if (type.equals(("sub"))) {

tomrrow = new Date(temp.getTime() - 24 * 60 * 60 * 1000 * num);

}

} catch (ParseException e) {

System.out.println(e.getMessage());

return null;

}

return dateformat.format(tomrrow);

}

/**

* 在一个日期上,package com.util;import java.text.ParseException;import java.text.ParsePosition;import java.text.SimpleDateFormat;import java.util.Calenda加减一定天数.返回一个新的日期

*

* @param 日期字符串

* @param type日期类型:add

* 向前 sub向后

* @param 天数

* @return

*/

public static String getDate(String date, String type, int num) {

Date tomrrow = null;

SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");

try {

Date temp = dateformat.parse(date);

if (type.equals(("add"))) {

tomrrow = new Date(temp.getTime() + 24 * 60 * 60 * 1000 * num);

} else if (type.equals(("sub"))) {

tomrrow = new Date(temp.getTime() - 24 * 60 * 60 * 1000 * num);

}

} catch (ParseException e) {

System.out.println(e.getMessage());

return null;

}

return dateformat.format(tomrrow);

}

/**

* 在一个日期上,加减一定天数.返回一个新的日期

*

* @param 日期字符串

* @param type日期类型:add

* 向前 sub向后

* @param 天数

* @return

*/

public static String getForMatDate(String date, String type, int num) {

Date tomrrow = null;

SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");

try {

Date temp = dateformat.parse(date);

if (type.equals(("add"))) {

tomrrow = new Date(temp.getTime() + 24 * 60 * 60 * 1000 * num);

} else if (type.equals(("sub"))) {

tomrrow = new Date(temp.getTime() - 24 * 60 * 60 * 1000 * num);

}

} catch (ParseException e) {

System.out.println(e.getMessage());

return null;

}

return dateformat.format(tomrrow);

}

/**

* 根据一个日期得到星期几

*

* @param 日期

* @return

*/

public static String getWeekByDate(Date date) {

Calendar c = Calendar.getInstance();

c.setTime(date);

int dayOfWeek = 0;

String dayOfweekStr = "";

dayOfWeek = c.get(Calendar.DAY_OF_WEEK);

// 根据日期date得到是星期几

switch (dayOfWeek) {

case 1:

dayOfweekStr = "日";

break;

case 2:

dayOfweekStr = "一";

break;

case 3:

dayOfweekStr = "二";

break;

case 4:

dayOfweekStr = "三";

break;

case 5:

dayOfweekStr = "四";

break;

case 6:

dayOfweekStr = "五";

break;

case 7:

dayOfweekStr = "六";

break;

}

return dayOfweekStr;

}

/**

* 判断两个日期相隔多少天

*

* @param 日期

* @return

*/

public static int getBetWeenDateNum(String startDate, String endDate) {

int year1 = Integer.parseInt(startDate.substring(0, 4));

int month1 = Integer.parseInt(startDate.substring(4, 6));

int day1 = Integer.parseInt(startDate.substring(6, 8));

int year2 = Integer.parseInt(endDate.substring(0, 4));

int month2 = Integer.parseInt(endDate.substring(4, 6));

int day2 = Integer.parseInt(endDate.substring(6, 8));

Calendar c1 = Calendar.getInstance();

c1.set(year1, month1 - 1, day1);

Calendar c2 = Calendar.getInstance();

c2.set(year2, month2 - 1, day2);

int betWeenDays = (int) ((c2.getTimeInMillis() - c1.getTimeInMillis()) / (1000 * 60 * 60 * 24));

return betWeenDays;

}

/**

* 日期增加天数后得到新日期

*

* @param 日期

* @return

*/

public static String getNewDate(String dateStr, int addDay) {

SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");

Date date = null;

try {

date = dateformat.parse(dateStr);

} catch (ParseException e) {

e.printStackTrace();

}

Calendar c = Calendar.getInstance();

c.setTime(date);

c.add(Calendar.DATE, addDay);

date = c.getTime();

String newDate = dateformat.format(date);

return newDate;

}

}

/* * Created on 2011-5-26 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值