java入门、java学习:java日期格式处理类,《上》

来源:互联网 发布:nginx 域名解析 编辑:程序博客网 时间:2024/05/02 04:42

/**

* 主要用于对Java日期的基本操作 <br>

* 以下为Java日期对应的各个时间位所对应的代号,如一年中的某一天6,一天中的某时11等 <br>

* DAY_OF_WEEK 0x7(7) <br>

* DAY_OF_YEAR 0x6(6) <br>

* HOUR_OF_DAY 0xb(11) <br>

* DAY_OF_MONTH 0x5(5) <br>

* YEAR 0x1(1) <br>

* WEEK_OF_MONTH 0x4(4) <br>

* WEEK_OF_YEAR 0x3(3) <br>

* MINITE 0xc(12) <br>

* SECOND 0xd(13) <br>

* MILLISECOND 0xe(14) <br>

*

* @author Rock

*

*/

public class DateUtils {

// 默认的时间格式

public final static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

// 年-月

public final static String YEAR_AND_MONTH = "yyyy-MM"; // 年-月-日 时:分

public final static String DEFAULT_DATE_FORMAT_MI = "yyyy-MM-dd HH:mm";

// 年-月-日

public final static String DEFAULT_SHORT_DATE = "yyyy-MM-dd"; // 年月日

public final static String MINI_DATE_SEPARATOR_NONE = "yyMMdd";

// public final static long HOUR_TIME_MILLIS = 3600000;

// public final static long DAY_TIME_MILLIS = 24 *

HOUR_TIME_MILLIS;

/**

* 将时间转换为Date,默认格式为-MM-HH:mm:ss)

*

* @param formatStr

* @return

*/

public static Date strToDate(String str, String formatStr) { Date date = null;

SimpleDateFormat format = new

SimpleDateFormat(isEmpty(formatStr) ? DEFAULT_DATE_FORMAT : formatStr);

try {

date = format.parse(str);

}

catch (Exception e) {}

return date;

}

/**

* 判断字符串是否为空

*

* @param str

* @return

*/

private static boolean isEmpty(String str) {

if (str == null || str.trim().equals("")) {

return true;

}

return false;

}

/**

* 将Date格式的日期转化为制定格式的字符串<br>

* 默认格式为-MM- *

* @param date

* @param formatStr

* @return

*/

public static String dateToStr(Date date, String formatStr) { String str = "";

SimpleDateFormat format = null;

if (formatStr == null || formatStr.equals("")) {

format = new SimpleDateFormat(DEFAULT_DATE_FORMAT); } else {

format = new SimpleDateFormat(formatStr);

}

try {

str = format.format(date);

}

catch (Exception e) {}

return str;

}

/**

* 判断今天是否在某个日期之后 <br>

* 默认格式为-MM- *

* @param dateStr

* @return

*/

public static Boolean afterNow(String dateStr, String format) {

try {

Date date = strToDate(dateStr, isEmpty(format) ? DEFAULT_DATE_FORMAT : format);

Date nowDate = new Date();

return nowDate.after(date);

}

catch (Exception e) {

return false;

}

}

/**

* 判断一个日期是否为当前日期时间,比如是否为今天2016-09-30<br> * 默认格式为-MM-

*

* @param date

* @param format

* @return

*/

public static Boolean isDateNow(Date date, String format) { try {

String formatT = isEmpty(format) ? DEFAULT_SHORT_DATE : format;

String currentTime = new

SimpleDateFormat(formatT).format(new Date());

String postTime = new

SimpleDateFormat(formatT).format(date);

if (currentTime.equals(postTime)) {

return true;

} else {

return false;

}

}

catch (Exception e) {

return false;

}

}

// ���ڼ�������

public static String addDateToStr(Date s, int days, String formatStr) {

GregorianCalendar cal = new GregorianCalendar(); cal.setTime(s);

cal.add(Calendar.DATE, days);

return DateUtils.dateToStr(cal.getTime(), formatStr); }

/**

* 获取某个日期前N天或者后N天的日期

*

* @param date

* @param days

* 正数表示该日期之后,负数表示之前

* @return

*/

public static Date addDate(Date date, int days) { GregorianCalendar cal = new GregorianCalendar(); cal.setTime(date);

cal.add(Calendar.DATE, days);

return cal.getTime();

}

/**

* 通过日期获取是该年的第几周

*

* @param date

* @return

*/

public static int getWeekOfYear(Date date) { Calendar cal = Calendar.getInstance();

cal.setTime(date);

return cal.get(Calendar.WEEK_OF_YEAR);

}

/**

* 获取一个日期是一周中的第几天(周日为第一天)

*

* @param date

* @return

*/

public static int getDayOfWeek(Date date) { Calendar cal = Calendar.getInstance();

cal.setTime(date);

return cal.get(Calendar.DAY_OF_WEEK);

}

/**

* 获取本月的第一天

*

* @return

*/

public static Date getMonthFirstDay() {

return DateUtils.strToDate(Calendar.YEAR + "-" + Calendar.MONTH + "-01 00:00:00", DEFAULT_DATE_FORMAT); }

/**

* 获取本月的最后一天

*

* @return

*/

public static Date getMonthLastDay() {

Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH,

calendar.getActualMaximum(Calendar.DAY_OF_MONTH));

return calendar.getTime();

}

对于学习有困难不知道如何提升自己可以加扣:578024144 进行交流得到帮助,获取学习资料

0 0
原创粉丝点击