Java日期获取需求大全

刚进公司,作为熟悉技术,为公司做了一个小的点餐系统的网站,其中大量用到了时间日期作为唯一标示或是显示设置。特总结了一下和大家分享。

package com.lucis.ordering.Utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
* Created by JayZhao on 2015/8/17 0017.
* 系统日期获取控件
*/
public class DateUtil {

//实例化date对象
Date date = new Date();
//格式化成年月日式 date 类型
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//格式化年月日 时分秒 date 类型
SimpleDateFormat detailsDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//解析成年-月-日 格式date形数据
public Date SimpleSystemTime(){
String now =new SimpleDateFormat("yyyy-MM-dd").format(date);
try {
return simpleDateFormat.parse(now);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
//解析成年-月-日 格式String形数据
public String SimpleSystemTimeString(){
return new SimpleDateFormat("yyyy-MM-dd").format(date);
}
//解析成年-月-日 时:分:秒 格式date形数据
public Date DetailSystemTime(){
String now =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
try {
return detailsDateFormat.parse(now);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
//解析成年-月-日 星期几 格式date形数据
public String MiddleSystemTime() {
String now =new SimpleDateFormat("yyyy-MM-dd").format(date);
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0) {
w = 0;
}
return now+" "+weekDays[w];
}
//根据时间获取当日星期
public int WeekDays(String day){

Calendar cal = Calendar.getInstance();
try {
cal.setTime(simpleDateFormat.parse(day));
} catch (ParseException e) {
e.printStackTrace();
}
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
return w;
}
//解析当前日期前几天成 年-月-日 格式的date数据
public String Yesterday(){
Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
calendar.add(Calendar.DATE, -1); //得到前一天
return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
}
//解析某一天的前几天
public String getSpecifiedDayBefore(String specifiedDay,int d){
Calendar c = Calendar.getInstance();
Date date=null;
try {
date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day=c.get(Calendar.DATE);
c.set(Calendar.DATE,day-d);
String dayBefore=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
return dayBefore;
}
//比较两个时间大小
public boolean compare(String date1,String date2){
boolean result = false;
try {
result = simpleDateFormat.parse(date1).before(simpleDateFormat.parse(date2));
} catch (ParseException e) {
e.printStackTrace();
}
return result;
}
//根据一天获取一周时间
public String testDate(String newtime) {
String dayNames[] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
int week1[] = {7,1,2,3,4,5,6};
Calendar c = Calendar.getInstance();// 获得一个日历的实例
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
c.setTime(sdf.parse(newtime));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String a=dayNames[c.get(Calendar.DAY_OF_WEEK)-1];
int b=week1[c.get(Calendar.DAY_OF_WEEK)-1];
if(b!=7){
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
}else{
c.add(Calendar.WEEK_OF_MONTH, -1);
c.set(Calendar.DAY_OF_WEEK, 2);
}
return a;
}

//获取某一天的下周一
public String getNextMonday(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int week = cal.get(Calendar.DAY_OF_WEEK);
if(week>2){
cal.add(Calendar.DAY_OF_MONTH,-(week-2)+7);
}else{
cal.add(Calendar.DAY_OF_MONTH,2-week+7);
}
return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
}

//获取某一天的本周一
public String getMonday(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int week = cal.get(Calendar.DAY_OF_WEEK);
if(week>2){
cal.add(Calendar.DAY_OF_MONTH,-(week-2));
}else{
cal.add(Calendar.DAY_OF_MONTH,2-week);
}
return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
}
}

你可能感兴趣的:(Java日期获取需求大全)