获取当前时间和某个时间进行比较此时主要拿long型的时间值
方法如下
要使用 javautilDate 获取当前时间的代码如下
代码如下
Date date = new Date();
dategetTime() ;
还有一种方式使用 SystemcurrentTimeMillis() ;
都是得到一个当前的时间的long型的时间的毫秒值这个值实际上是当前时间值与年一月一号零时零分零秒相差的毫秒数
一获取当前时间 格式为: yyyymmdd hhmmss
DateFormatgetDateTimeInstance( LocaleCHINESE)format(new javautilDate());
二获取当前时间 格式为: yyyy年mm月dd日 上午/下午hh时mm分ss秒
代码如下
DateFormatgetDateTimeInstance(DateFormatLONG DateFormatLONG LocaleCHINESE)format(new javautilDate());
三获取当前时间(精确到毫秒) 格式为: yyyymmdd hh:mm:ssnnn
代码如下
new javasqlTimestamp(SystemcurrentTimeMillis())toString();
一 获取当前系统时间和日期并格式化输出:
代码如下
import javautilDate;
import javatextSimpleDateFormat;
public class NowString {
public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss");//设置日期格式
Systemoutprintln(dfformat(new Date()));// new Date()为获取当前系统时间
}
}
二 在数据库里的日期只以年月日的方式输出可以用下面两种方法
用convert()转化函数
代码如下
String sqlst = "select convert(varchar()bookDate) as convertBookDate from roomBook where bookDate between and ";
Systemoutprintln(rsgetString("convertBookDate"));
利用SimpleDateFormat类
先要输入两个java包
import javautilDate;
import javatextSimpleDateFormat;
然后
定义日期格式SimpleDateFormat sdf = new SimpleDateFormat(yyMMdd);
sql语句为String sqlStr = "select bookDate from roomBook where bookDate between and ";
输出
Systemoutprintln(dfformat(rsgetDate("bookDate")));
java中获取当前日期和时间的方法
代码如下
import javautilDate;
import javautilCalendar;
import javatextSimpleDateFormat;
public class TestDate{
public static void main(String[] args){
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式
String hehe = dateFormatformat( now );
Systemoutprintln(hehe);
Calendar c = CalendargetInstance();//可以对每个时间域单独修改
int year = cget(CalendarYEAR);
int month = cget(CalendarMONTH);
int date = cget(CalendarDATE);
int hour = cget(CalendarHOUR_OF_DAY);
int minute = cget(CalendarMINUTE);
int second = cget(CalendarSECOND);
Systemoutprintln(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second);
}
}
有时候要把String类型的时间转换为Date类型通过以下的方式就可以将你刚得到的时间字符串转换为Date类型了
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
javautilDate time=null;
try {
time= sdfparse(sdfformat(new Date()));
} catch (ParseException e) {
eprintStackTrace();
}
一个不错日期时间类
代码如下
package comwondersgroupcccti;
import javasqlTimestamp;
import javautilCalendar;
import javautilDate;
public class TestGetSystemTime {
static Date nowTime = null;
/**
* @param args
*/
public static void main(String[] args) {
// 以下为 Clendar 测试代码
int yyyy mm dd;
Calendar c = CalendargetInstance();
yyyy = cget(CalendarYEAR);
mm = cget(CalendarMONTH) + ;
dd = cget(CalendarDAY_OF_MONTH);
Systemoutprintln("year = "+yyyy);
Systemoutprintln("month = "+mm);
Systemoutprintln("day = "+dd);
// SystemcurrentTimeMillis() 获取系统时间的代码
Timestamp ts = new Timestamp(SystemcurrentTimeMillis());
String dateStr = tstoString();
Systemoutprintln("SystemcurrentTimeMillis() = "+dateStr);
// date 为获得系统时间的另外一种方法
Date date = new Date();
String dateStr = new Timestamp(dategetTime())toString();
Systemoutprintln(dateStr);
// 得到JAVA运行环境的一些基本属性
SystemgetProperties()list(Systemout);
}
}
一个完整的获取日期实例
格式化日期 String>Date 或者 Data>String
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = sdfparse("");//String>Date
String sdate = sdfformat(date );// Data>String ===============================================================
package comhefengtest;
import javatextDateFormat;
import javatextParsePosition;
import javatextSimpleDateFormat;
import javautilCalendar;
import javautilDate;
import javautilGregorianCalendar;
public class TimeTest {
//用来全局控制 上一周本周下一周的周数变化
private int weeks = ;
private int MaxDate;//一月最大天数
private int MaxYear;//一年最大天数
/**
* @param args
*/
public static void main(String[] args) {
TimeTest tt = new TimeTest();
Systemoutprintln("得到个月后的日期:"+tt getAfterMonth();
Systemoutprintln("获取当天日期:"+ttgetNowTime("yyyyMMdd"));
Systemoutprintln("获取本周一日期:"+ttgetMondayOFWeek());
Systemoutprintln("获取本周日的日期~:"+ttgetCurrentWeekday());
Systemoutprintln("获取上周一日期:"+ttgetPreviousWeekday());
Systemoutprintln("获取上周日日期:"+ttgetPreviousWeekSunday());
Systemoutprintln("获取下周一日期:"+ttgetNextMonday());
Systemoutprintln("获取下周日日期:"+ttgetNextSunday());
Systemoutprintln("获得相应周的周六:"+ttgetNowTime("yyyyMMdd"));
Systemoutprintln("获取本月第一天日期:"+ttgetFirstDayOfMonth());
Systemoutprintln("获取本月最后一天日期:"+ttgetDefaultDay());
Systemoutprintln("获取上月第一天日期:"+ttgetPreviousMonthFirst());
Systemoutprintln("获取上月最后一天的日期:"+ttgetPreviousMonthEnd());
Systemoutprintln("获取下月第一天日期:"+ttgetNextMonthFirst());
Systemoutprintln("获取下月最后一天日期:"+ttgetNextMonthEnd());
Systemoutprintln("获取本年的第一天日期:"+ttgetCurrentYearFirst());
Systemoutprintln("获取本年最后一天日期:"+ttgetCurrentYearEnd());
Systemoutprintln("获取去年的第一天日期:"+ttgetPreviousYearFirst());
Systemoutprintln("获取去年的最后一天日期:"+ttgetPreviousYearEnd());
Systemoutprintln("获取明年第一天日期:"+ttgetNextYearFirst());
Systemoutprintln("获取明年最后一天日期:"+ttgetNextYearEnd());
Systemoutprintln("获取本季度第一天到最后一天:"+ttgetThisSeasonTime());
Systemoutprintln("获取两个日期之间间隔天数~
:"+TimeTestgetTwoDay(""""));
}
/**
* 得到指定月后(前)的日期 参数传负数即可
*/
public static String getAfterMonth(int month) {
Calendar c = CalendargetInstance();//获得一个日历的实例
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = null;
try{
date = sdfparse("");//初始日期
}catch(Exception e){
}
csetTime(date);//设置日历时间
cadd(CalendarMONTHmonth);//在日历的月份上增加个月
String strDate = sdfformat(cgetTime()));//的到你想要得个月后的日期
return strDate;
}
/**
* 得到二个日期间的间隔天数
*/
public static String getTwoDay(String sj String sj) {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyyMMdd");
long day = ;
try {
javautilDate date = myFormatterparse(sj);
javautilDate mydate = myFormatterparse(sj);
day = (dategetTime() mydategetTime()) / ( * * * );
} catch (Exception e) {
return "";
}
return day + "";
}
/**
* 根据一个日期返回是星期几的字符串
*/
public static String getWeek(String sdate) {
// 再转换为时间
Date date = TimeTeststrToDate(sdate);
Calendar c = CalendargetInstance();
csetTime(date);
// int hour=cget(CalendarDAY_OF_WEEK);
// hour 中存的就是星期几了其范围 ~
// =星期日 =星期六其他类推
return new SimpleDateFormat("EEEE")format(cgetTime());
}
/**
* 将短时间格式字符串转换为时间 yyyyMMdd
*/
public static Date strToDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
ParsePosition pos = new ParsePosition();
Date strtodate = formatterparse(strDate pos);
return strtodate;
}
/**
* 两个时间之间的天数
*
* @param date
* @param date
* @return
*/
public static long getDays(String date String date) {
if (date == null || dateequals(""))
return ;
if (date == null || dateequals(""))
return ;
// 转换为标准时间
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyyMMdd");
javautilDate date = null;
javautilDate mydate = null;
try {
date = myFormatterparse(date);
mydate = myFormatterparse(date);
} catch (Exception e) {
}
long day = (dategetTime() mydategetTime()) / ( * * * );
return day;
}
// 计算当月最后一天返回字符串
public String getDefaultDay(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar lastDate = CalendargetInstance();
lastDateset(CalendarDATE);//设为当前月的 号
lastDateadd(CalendarMONTH);//加一个月变为下月的 号
lastDateadd(CalendarDATE);//减去一天变为当月最后一天
str=sdfformat(lastDategetTime());
return str;
}
// 上月第一天
public String getPreviousMonthFirst(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar lastDate = CalendargetInstance();
lastDateset(CalendarDATE);//设为当前月的 号
lastDateadd(CalendarMONTH);//减一个月变为下月的 号
//lastDateadd(CalendarDATE);//减去一天变为当月最后一天
str=sdfformat(lastDategetTime());
return str;
}
//获取当月第一天
public String getFirstDayOfMonth(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar lastDate = CalendargetInstance();
lastDateset(CalendarDATE);//设为当前月的 号
str=sdfformat(lastDategetTime());
return str;
}
// 获得本周星期日的日期
public String getCurrentWeekday() {
weeks = ;
int mondayPlus = thisgetMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDateadd(GregorianCalendarDATE mondayPlus+);
Date monday = currentDategetTime();
DateFormat df = DateFormatgetDateInstance();
String preMonday = dfformat(monday);
return preMonday;
}
//获取当天时间
public String getNowTime(String dateformat){
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);//可
以方便
地修改日期格式
String hehe = dateFormatformat(now);
return hehe;
}
// 获得当前日期与本周日相差的天数
private int getMondayPlus() {
Calendar cd = CalendargetInstance();
// 获得今天是一周的第几天星期日是第一天星期二是第二天
int dayOfWeek = cdget(CalendarDAY_OF_WEEK); //因为按中国礼拜一
作为第一
天所以这里减
if (dayOfWeek == ) {
return ;
} else {
return dayOfWeek;
}
}
//获得本周一的日期
public String getMondayOFWeek(){
weeks = ;
int mondayPlus = thisgetMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDateadd(GregorianCalendarDATE mondayPlus);
Date monday = currentDategetTime();
DateFormat df = DateFormatgetDateInstance();
String preMonday = dfformat(monday);
return preMonday;
}
//获得相应周的周六的日期
public String getSaturday() {
int mondayPlus = thisgetMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDateadd(GregorianCalendarDATE mondayPlus + * weeks + );
Date monday = currentDategetTime();
DateFormat df = DateFormatgetDateInstance();
String preMonday = dfformat(monday);
return preMonday;
}
// 获得上周星期日的日期
public String getPreviousWeekSunday() {
weeks=;
weeks;
int mondayPlus = thisgetMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDateadd(GregorianCalendarDATE mondayPlus+weeks);
Date monday = currentDategetTime();
DateFormat df = DateFormatgetDateInstance();
String preMonday = dfformat(monday);
return preMonday;
}
// 获得上周星期一的日期
public String getPreviousWeekday() {
weeks;
int mondayPlus = thisgetMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDateadd(GregorianCalendarDATE mondayPlus + * weeks);
Date monday = currentDategetTime();
DateFormat df = DateFormatgetDateInstance();
String preMonday = dfformat(monday);
return preMonday;
}
// 获得下周星期一的日期
public String getNextMonday() {
weeks++;
int mondayPlus = thisgetMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDateadd(GregorianCalendarDATE mondayPlus + );
Date monday = currentDategetTime();
DateFormat df = DateFormatgetDateInstance();
String preMonday = dfformat(monday);
return preMonday;
}
// 获得下周星期日的日期
public String getNextSunday() {
int mondayPlus = thisgetMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDateadd(GregorianCalendarDATE mondayPlus + +);
Date monday = currentDategetTime();
DateFormat df = DateFormatgetDateInstance();
String preMonday = dfformat(monday);
return preMonday;
}
private int getMonthPlus(){
Calendar cd = CalendargetInstance();
int monthOfNumber = cdget(CalendarDAY_OF_MONTH);
cdset(CalendarDATE );//把日期设置为当月第一天
cdroll(CalendarDATE );//日期回滚一天也就是最后一天
MaxDate=cdget(CalendarDATE);
if(monthOfNumber == ){
return MaxDate;
}else{
return monthOfNumber;
}
}
//获得上月最后一天的日期
public String getPreviousMonthEnd(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar lastDate = CalendargetInstance();
lastDateadd(CalendarMONTH);//减一个月
lastDateset(CalendarDATE );//把日期设置为当月第一天
lastDateroll(CalendarDATE );//日期回滚一天也就是本月最后一天
str=sdfformat(lastDategetTime());
return str;
}
//获得下个月第一天的日期
public String getNextMonthFirst(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar lastDate = CalendargetInstance();
lastDateadd(CalendarMONTH);//减一个月
lastDateset(CalendarDATE );//把日期设置为当月第一天
str=sdfformat(lastDategetTime());
return str;
}
//获得下个月最后一天的日期
public String getNextMonthEnd(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar lastDate = CalendargetInstance();
lastDateadd(CalendarMONTH);//加一个月
lastDateset(CalendarDATE );//把日期设置为当月第一天
lastDateroll(CalendarDATE );//日期回滚一天也就是本月最后一天
str=sdfformat(lastDategetTime());
return str;
}
//获得明年最后一天的日期
public String getNextYearEnd(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar lastDate = CalendargetInstance();
lastDateadd(CalendarYEAR);//加一个年
lastDateset(CalendarDAY_OF_YEAR );
lastDateroll(CalendarDAY_OF_YEAR );
str=sdfformat(lastDategetTime());
return str;
}
//获得明年第一天的日期
public String getNextYearFirst(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar lastDate = CalendargetInstance();
lastDateadd(CalendarYEAR);//加一个年
lastDateset(CalendarDAY_OF_YEAR );
str=sdfformat(lastDategetTime());
return str;
}
//获得本年有多少天
private int getMaxYear(){
Calendar cd = CalendargetInstance();
cdset(CalendarDAY_OF_YEAR);//把日期设为当年第一天
cdroll(CalendarDAY_OF_YEAR);//把日期回滚一天
int MaxYear = cdget(CalendarDAY_OF_YEAR);
return MaxYear;
}
private int getYearPlus(){
Calendar cd = CalendargetInstance();
int yearOfNumber = cdget(CalendarDAY_OF_YEAR);//获得当天是一年中的第几天
cdset(CalendarDAY_OF_YEAR);//把日期设为当年第一天
cdroll(CalendarDAY_OF_YEAR);//把日期回滚一天
int MaxYear = cdget(CalendarDAY_OF_YEAR);
if(yearOfNumber == ){
return MaxYear;
}else{
return yearOfNumber;
}
}
//获得本年第一天的日期
public String getCurrentYearFirst(){
int yearPlus = thisgetYearPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDateadd(GregorianCalendarDATEyearPlus);
Date yearDay = currentDategetTime();
DateFormat df = DateFormatgetDateInstance();
String preYearDay = dfformat(yearDay);
return preYearDay;
}
//获得本年最后一天的日期 *
public String getCurrentYearEnd(){
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方
便地修
改日期格式
String years = dateFormatformat(date);
return years+"";
}
//获得上年第一天的日期 *
public String getPreviousYearFirst(){
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方
便地修
改日期格式
String years = dateFormatformat(date); int years_value = IntegerparseInt
(years);
years_value;
return years_value+"";
}
//获得上年最后一天的日期
public String getPreviousYearEnd(){
weeks;
int yearPlus = thisgetYearPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDateadd(GregorianCalendarDATEyearPlus+MaxYear*weeks+(MaxYear));
Date yearDay = currentDategetTime();
DateFormat df = DateFormatgetDateInstance();
String preYearDay = dfformat(yearDay);
getThisSeasonTime();
return preYearDay;
}
//获得本季度
public String getThisSeasonTime(int month){
int array[][] = {{}{}{}{}};
int season = ;
if(month>=&&month<=){
season = ;
}
if(month>=&&month<=){
season = ;
}
if(month>=&&month<=){
season = ;
}
if(month>=&&month<=){
season = ;
}
int start_month = array[season][];
int end_month = array[season][];
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方
便地修
改日期格式
String years = dateFormatformat(date);
int years_value = IntegerparseInt(years);
int start_days =;//years+""+StringvalueOf(start_month)+"
";//getLastDayOfMonth(years_valuestart_month);
int end_days = getLastDayOfMonth(years_valueend_month);
String seasonDate =
years_value+""+start_month+""+start_days+";"+years_value+""+end_month+""+end_days;
return seasonDate;
}
/**
* 获取某年某月的最后一天
* @param year 年
* @param month 月
* @return 最后一天
*/
private int getLastDayOfMonth(int year int month) {
if (month == || month == || month == || month == || month ==
|| month == || month == ) {
return ;
}
if (month == || month == || month == || month == ) {
return ;
}
if (month == ) {
if (isLeapYear(year)) {
return ;
} else {
return ;
}
}
return ;
}
/**
* 是否闰年
* @param year 年
* @return
*/
public boolean isLeapYear(int year) {
return (year % == && year % != ) || (year % == );
}