开发过程中,经常会使用到date数据类型的转换,这里做一个总结归纳,以供参考!
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import org.springframework.ui.ModelMap;
/**
* 日期、星期类型转换
* @author liang
*
*/
public class DateUtil {
public static void main(String[] args) throws ParseException{
// SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
// java.util.Date date= myFormatter.parse("1997-04-30");
// long nowTime = System.currentTimeMillis();
// long dateTime = date.getTime();// 得到毫秒数
// long subHours = (nowTime - dateTime)/365/24/60/60/1000;// 取得相差分钟数;
// SimpleDateFormat myFormatter = new SimpleDateFormat("yyyyMMdd");
// java.util.Date date= myFormatter.parse("20101203");
// String subHours=formartDate(date);
// String week=getDayOfWeek(date);
// System.out.println(subHours+"-----"+week);
String date = getCurDate(DateUtil.yearMonthDayFor24);
System.out.println(date.toString());
System.out.println(getCurDateTime());
}
public final static String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
public final static String dayIndexs[] = { "7", "1", "2", "3", "4", "5","6" };
/**
* 星期数字中文键值对
*/
public static final Map<String, String> WEEK_MAP = new HashMap<String, String>();
static {
WEEK_MAP.put(dayNames[0], dayIndexs[0]);
WEEK_MAP.put(dayNames[1], dayIndexs[1]);
WEEK_MAP.put(dayNames[2], dayIndexs[2]);
WEEK_MAP.put(dayNames[3], dayIndexs[3]);
WEEK_MAP.put(dayNames[4], dayIndexs[4]);
WEEK_MAP.put(dayNames[5], dayIndexs[5]);
WEEK_MAP.put(dayNames[6], dayIndexs[6]);
}
/**
* 年月日格式yyyy-MM-dd
*/
public final static String yearMonthDay = "yyyy-MM-dd";
/**
* 年月日格式yyyyMMdd
*/
public final static String yearMonthDaySingle = "yyyyMMdd";
/**
* 年月日 时分秒格式"yyyy-MM-dd HH:mm:ss"
*/
public final static String yearMonthDayFor24 = "yyyy-MM-dd HH:mm:ss";
/**
* 年月日 中文格式yyyy年MM月dd日
*/
public final static String yearMonthDayForChina="yyyy年MM月dd日";
/**
* 24小时制时分秒格式"HH:mm:ss"
*/
public final static String hourMinuteSecond = "HH:mm:ss";
/**
* 12小时制时分秒格式"hh:mm:ss
*/
public final static String hourMinuteSecondFor12 = "hh:mm:ss";
// private static Log log= LogFactory.getLog(Util.class);
/** 取得当前时间与传入时间进行比较相差分钟数 */
public static long getSubMinutes(Date date) {
long nowTime = System.currentTimeMillis();
long dateTime = date.getTime();// 得到毫秒数
long subHours = (nowTime - dateTime)/60/1000;// 取得相差分钟数;
return subHours;
}
/** 取得明天起一周的map日期(格式为'星期或日期')*/
public static Map<String,String> getNextWeekFromTomorrow(String formatType) {
Map<String,String> map = new TreeMap<String,String>();// map 排序
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(formatType);
java.util.Calendar calendar = java.util.Calendar.getInstance();
//TODO: 修改以便可以预约当天的号源,到时候需要修改
for (int i = 1; i < 8; i++) {
calendar.add(java.util.Calendar.DAY_OF_YEAR, i==0?0:1);
String date = df.format(calendar.getTime());
String weekdayindex = DateUtil.getDayIndexOfWeek(calendar.getTime());
String weekday = DateUtil.getDayOfWeek(calendar.getTime());
map.put(weekdayindex+"week",weekday);
map.put(weekdayindex+"date",date);
}
return map;
}
/** 取得日期的星期(数字)*/
public static String getDayIndexOfWeek(Date date) {
Calendar cld = Calendar.getInstance();
cld.setTime(date);
int dayOfWeek = cld.get(Calendar.DAY_OF_WEEK);
return dayIndexs[dayOfWeek - 1];
}
/** 取得日期的星期(汉字)*/
public static String getDayOfWeek(Date date) {
Calendar cld = Calendar.getInstance();
cld.setTime(date);
int dayOfWeek = cld.get(Calendar.DAY_OF_WEEK);
return dayNames[dayOfWeek - 1];
}
/**将指定格式的字符串转化为日期类型*/
public static Date parseDate(String dateString,String formatStr){
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(formatStr);
Date date = null;
try{
date = df.parse(dateString);
}catch(ParseException ex){
ex.printStackTrace();
}
return date;
}
/**
* 获取指定类型的日期
* @param formatStr
* @return
*/
public static String getCurDate(String formatStr){
SimpleDateFormat myFormatter = new SimpleDateFormat(formatStr);
java.util.Date date = new java.util.Date();
String curDate="";
curDate = myFormatter.format(date);
return curDate;
}
/**
* 获取系统当前时间
* @return
*/
public static Date getCurDateTime(){
return new java.util.Date(System.currentTimeMillis());
}
/**将日期类型转成格式为指定的字符串*/
public static String formartDate(Date date,String patten){
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(patten);
return df.format(date);
}
/**将日期类型转成格式为'yyyy年MM月dd日'的字符串*/
public static String formartDate(Date date){
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy年MM月dd日");
return df.format(date);
}
/**将日期类型转成格式为'yyyy年MM月dd日 HH:mm:ss'的字符串*/
public static String formartFullDate(Date date){
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String time = df.format(date);
return time;
}
public static String toHtml(String s){
StringBuilder b = new StringBuilder();
for(int i=0;i<s.length();i++){
char c = s.charAt(i);
if(c=='\n')
b.append("<br>");
else if(c=='\r'){
b.append("<br>");
if(s.charAt(i+1)=='\n'){
i++;
}
}else if(c=='<'){
b.append("<");
}else if(c=='>'){
b.append(">");
}else
b.append(c);
}
return b.toString();
}
/**取出传入日期的小时数*/
public static int getHour(Date date) {
Calendar cld = Calendar.getInstance();
cld.setTime(date);
return cld.get(Calendar.HOUR_OF_DAY);
}
public static int getAsciiLen(String s){
int len = 0;
for(int i=0;i<s.length();i++){
int c = s.charAt(i);
if(c>255){
len +=2;
}else
len ++;
}
return len;
}
public static String substring(String s,int asciiLen){
StringBuffer tmp = new StringBuffer();
int len = 0;
for(int i=0;i<s.length();i++){
int c = s.charAt(i);
if(c>255){
len +=2;
}else
len ++;
if(len<=asciiLen){
tmp.append(s.charAt(i));
}
}
return tmp.toString();
}
/**
* @param str 日期:19970430
* @return 1997年04月30日
* @throws ParseException
* @author qiuyujia
*/
public static String getChinaDate(String str) throws ParseException{
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyyMMdd");
java.util.Date date= myFormatter.parse(str);
String value=formartDate(date);
return value;
}
/**
* @param str 日期:20101203
* @return 星期五
* @throws ParseException
* @author qiuyujia
*/
public static String getChinaWeek(String str) throws ParseException{
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyyMMdd");
java.util.Date date= myFormatter.parse(str);
String value=getDayOfWeek(date);
return value;
}
/**
* 用户使用的预约界面,显示七天(明天开始)
*
* @param type
* @param maps
*/
public static void setDateMap(int type, ModelMap maps) {
String dd[] = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
java.util.Calendar calendar = java.util.Calendar.getInstance();
Map<String, String> map = new TreeMap<String, String>();
Map dateIndex = new TreeMap();
int t = 1;
for (int i = 1; i < 8; i++) {
int indexflag = Integer.parseInt(DateUtil
.getDayIndexOfWeek(new Date()))
+ i;
if (indexflag > 7) {
indexflag = indexflag - 7;
}
calendar.add(java.util.Calendar.DAY_OF_YEAR, 1);
String date = df.format(calendar.getTime());
if (!(type == 2 && (indexflag == 6 || indexflag == 7))) {// 科室挂号去掉星期六星期日
map.put(date, dd[indexflag - 1]);
dateIndex.put(indexflag, date);// 设置星期索引,这样可以根据排班SchemeWeek来找到相应的日期
}
}
maps.addAttribute("date", map);
maps.addAttribute("dateIndex", dateIndex);
}
}