📄 dateutil.java
字号:
package com.myoa.cal.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.myoa.cal.model.CalDate;
public class DateUtil {
public java.sql.Timestamp StrToDate(String time){
DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date Udate = null;
java.sql.Timestamp Sdate = null;
try {
Udate = dateformat.parse(time);
Sdate = new java.sql.Timestamp(Udate.getTime());
}catch(Exception e){
}
if(Udate != null){
return Sdate;
}else{
return null;
}
}
public String StrToStrDate(String time){
DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat dateformat1 = new SimpleDateFormat("yyyy-M-d");
Date Udate = null;
java.sql.Date Sdate = null;
String date0 = null;
try {
Udate = dateformat.parse(time);
date0 = dateformat1.format(Udate);
}catch(Exception e){
}
return date0;
}
public String StrToStrTime(String time){
DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat dateformat1 = new SimpleDateFormat("yyyy-M-d H:m:s");
Date Udate = null;
java.sql.Date Sdate = null;
String date0 = null;
try {
Udate = dateformat1.parse(time);
date0 = dateformat.format(Udate);
}catch(Exception e){
}
return date0;
}
//获得年月格式的时间
public java.util.Date getYYMM(String yymm){
DateFormat dateformat = new SimpleDateFormat("yyyy-MM");
java.util.Date date = null;
try {
date = dateformat.parse(yymm);
} catch (ParseException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
if(date != null){
return date;
}else{
return null;
}
}
//把时间转化为字符串
public String DateTimeToStr(java.util.Date ts){
DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String date_str = dateformat.format(ts);
return date_str;
}
public java.sql.Date UDateToSDate(java.util.Date d){
java.sql.Date date = new java.sql.Date(d.getTime());
return date;
}
public String StringDate(String str1){
DateFormat dateformat = new SimpleDateFormat("yyyy-MM");
java.util.Date d;
String str2 = null;
try {
d = dateformat.parse(str1);
str2 = dateformat.format(d);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str2;
}
//获取所有的提示时间
public List remindTime(java.util.Date occurtime,int early_mm,int space_mm){
long early = early_mm * 60 * 1000;
long space = space_mm * 60 * 1000;
List<Date> list = new ArrayList<Date>();
java.util.Date remindtime = new Date(occurtime.getTime() - early);
list.add(remindtime);
Date spacetime = new Date(remindtime.getTime() + space);
while(spacetime.before(occurtime)){
Date addtime = new Date();
addtime.setTime(spacetime.getTime());
list.add(addtime);
spacetime.setTime(spacetime.getTime() + space);
}
return list;
}
public String getToday(){
DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String today = null;
try {
today = dateformat.format(date);
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return today;
}
public static void main(String[] args){
DateUtil util = new DateUtil();
// //System.out.println(util.StrToDate("2009-02-25 10:25:11"));
// List<CalDate> date = util.remindTime(util.StrToDate("2009-02-25 10:26:11"), 30, 5);
// System.out.println(date.size());
// for(int i=0;i<date.size();i++){
// System.out.println(date.get(i));
// }
Date d = util.StrToDate("2009-03-02 12:11:05");
String aa = "2009-03-02 12:11:05";
DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
java.sql.Timestamp dd=null;
dd = new java.sql.Timestamp(d.getTime());
System.out.println(d);
System.out.println(dd);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -