Java日期时间格式化操作DateUtils 的整理
直接上代码,总结了开发过程中经常用到的日期时间格式化操作!
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class DateUtils {
private static final String[] UNIT_DESC = new String[]{"天", "小时", "分钟", "秒"};
public static String currentYYYYMMDD() {
return getStrByDate(new Date(), "yyyyMMdd");
}
public static String currentHHMMSS() {
return getStrByDate(new Date(), "HHmmss");
}
public static String currentYYYYMMDDHHmmss() {
return getStrByDate(new Date(), "yyyyMMddHHmmss");
}
public static java.util.Date getDateByStr(String strDate, String format) throws ParseException {
assert strDate != null && format != null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
return simpleDateFormat.parse(strDate);
}
public static String getStrByDate(Date date, String format) {
assert date != null && format != null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
return simpleDateFormat.format(date);
}
public static Date getDayOfMonth() {
Calendar now = Calendar.getInstance();
return now.getTime();
}
public static Date getFirstDayOfMonth(Date date) {
Calendar nowday = Calendar.getInstance();
nowday.setTime(date);
nowday.set(Calendar.DATE, 1);// 把日期设置为当月第一天
return nowday.getTime();
}
public static Date getLastDayOfMonth(Date date) {
Calendar nowday = Calendar.getInstance();
nowday.setTime(date);
nowday.set(Calendar.DATE, 1);// 把日期设置为当月第一天
nowday.roll(Calendar.DATE, -1);// 日期回滚一天,也就是最后一天
return nowday.getTime();
}
public static String getCurrYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");
Date currYear = calendar.getTime();
return String.valueOf(dateFormat.format(currYear));
}
public static String getCurrMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
SimpleDateFormat dateFormat = new SimpleDateFormat("MM");
Date currMonth = calendar.getTime();
return String.valueOf(dateFormat.format(currMonth));
}
public static Date getLastDayByDate(Date d) {
Calendar newday = Calendar.getInstance();
newday.setTime(d);
int lastday;
int month = newday.get(Calendar.MONTH);
do {
lastday = newday.get(Calendar.DAY_OF_MONTH);
newday.add(Calendar.DAY_OF_MONTH, 1);
} while (newday.get(Calendar.MONTH) == month);
newday.set(Calendar.MONTH, month);
newday.set(Calendar.DAY_OF_MONTH, lastday);
return newday.getTime();
}
public static String formatyyyyMMdd(String dateString) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
Date date = simpleDateFormat.parse(dateString);
SimpleDateFormat formatStr = new SimpleDateFormat("yyyy-MM-dd");
return formatStr.format(date);
}
public static String formatyyyyMMddHHmmss(String dateString) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = simpleDateFormat.parse(dateString);
SimpleDateFormat formatStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatStr.format(date);
}
public static int getCurrYear() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");
Date currYearFirst = calendar.getTime();
return Integer.valueOf(dateFormat.format(currYearFirst));
}
public static Date getLastThreeMonths() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -3);
calendar.add(Calendar.DAY_OF_MONTH, 1);
return calendar.getTime();
}
public static Date getLastoneMonths() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
calendar.add(Calendar.DAY_OF_MONTH, 1);
return calendar.getTime();
}
public static Date getLastSixMonths() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -6);
calendar.add(Calendar.DAY_OF_MONTH, 1);
return calendar.getTime();
}
public static Date getCurrYearFirst(int year) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, year);
return calendar.getTime();
}
public static Date getCurrYearLast(int year) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, year);
calendar.roll(Calendar.DAY_OF_YEAR, -1);
return calendar.getTime();
}
public static String date2Str(Date date, String format) {
return getStrByDate(date, format);
}
public static String getSpecifiedDayBefore(Date date, String dateFormat){
if (date == null) return null;
Calendar c = Calendar.getInstance();
c.setTime(date);
int day=c.get(Calendar.DATE);
c.set(Calendar.DATE,day-1);
String dayBefore=new SimpleDateFormat(dateFormat).format(c.getTime());
return dayBefore;
}
public static String getSpecifiedDayAfter(Date date, String dateFormat) {
if (date == null) return null;
Calendar c = Calendar.getInstance();
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day + 1);
String dayAfter = new SimpleDateFormat(dateFormat).format(c.getTime());
return dayAfter;
}
public static String convertSeconds2Str(long seconds) {
StringBuilder sb = new StringBuilder();
long[] date = {TimeUnit.SECONDS.toDays(seconds), TimeUnit.SECONDS.toHours(seconds) % 24, TimeUnit.SECONDS.toMinutes(seconds) % 60, TimeUnit.SECONDS.toSeconds(seconds) % 60};
for (int i = 0; i < date.length; i++) {
long l = date[i];
if (l > 0) sb.append(l).append(UNIT_DESC[i]);
}
return sb.toString();
}
public static String convertMinute2Str(long minute) {
StringBuilder sb = new StringBuilder();
long[] date = {TimeUnit.SECONDS.toHours(minute) % 24,TimeUnit.SECONDS.toMinutes(minute) % 60, TimeUnit.SECONDS.toSeconds(minute) % 60};
for (int i = 0; i < date.length; i++) {
long l = date[i];
if (l > 0) sb.append(l).append(UNIT_DESC[i]);
}
return sb.toString();
}
}
以上就是关于java 日期格式化操作的所有内容,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!



