一 获取当前系统时间和日期并格式化输出:\x0d\\x0d\import javautilDate; \x0d\import javatextSimpleDateFormat;\x0d\\x0d\public class NowString { \x0d\ public static void main(String[] args) { \x0d\ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式\x0d\ Systemoutprintln(dfformat(new Date()));// new Date()为获取当前系统时间\x0d\ } \x0d\} \x0d\\x0d\二 在数据库里的日期只以年-月-日的方式输出,可以用下面两种方法:\x0d\\x0d\1、用convert()转化函数:\x0d\\x0d\String sqlst = "select convert(varchar(10),bookDate,126) as convertBookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";\x0d\\x0d\Systemoutprintln(rsgetString("convertBookDate")); \x0d\\x0d\2、利用SimpleDateFormat类:\x0d\\x0d\先要输入两个java包:\x0d\\x0d\import javautilDate; \x0d\import javatextSimpleDateFormat;\x0d\\x0d\然后:\x0d\\x0d\定义日期格式:SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd);\x0d\\x0d\sql语句为:String sqlStr = "select bookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";\x0d\\x0d\输出:\x0d\\x0d\Systemoutprintln(dfformat(rsgetDate("bookDate")));
在JAVA中获取当前时间的月份并转换成int型可以采用Calendar类提供的方法进行。
具体代码如下:
Calendar calendar=CalendargetInstance();//获得当前时间的月份,月份从0开始所以结果要加1
int month=calendarget(CalendarMONTH)+1;
人们俗称的“季度”,就是把一年平均分成四份,按照春、夏、秋、冬的顺序一年可以分为四个季度,每个季度历时3个月。第一季度:1月-3月第二季度:4月-6月第三季度:7月-9月第四季度:10月-12月而实际上严格的划分应该为:(按照中国的维度)第一季度为:3~5月(春季)第二季度为:6~8月(夏季)第三季度为:9~11月(秋季)第四季度为:12~2月(冬季)
有两种方法:
方法一:用javautilDate类来实现,并结合javatextDateFormat类来实现时间的格式化,看下面代码:
import javautil;
import javatext;
//以下默认时间日期显示方式都是汉语语言方式
//一般语言就默认汉语就可以了,时间日期的格式默认为MEDIUM风格,比如:2008-6-16 20:54:53
//以下显示的日期时间都是再Date类的基础上的来的,还可以利用Calendar类来实现见类TestDate2java
public class TestDate {
public static void main(String[] args) {
Date now = new Date();
Calendar cal = CalendargetInstance();
DateFormat d1 = DateFormatgetDateInstance(); //默认语言(汉语)下的默认风格(MEDIUM风格,比如:2008-6-16 20:54:53)
String str1 = d1format(now);
DateFormat d2 = DateFormatgetDateTimeInstance();
String str2 = d2format(now);
DateFormat d3 = DateFormatgetTimeInstance();
String str3 = d3format(now);
DateFormat d4 = DateFormatgetInstance(); //使用SHORT风格显示日期和时间
String str4 = d4format(now);
DateFormat d5 = DateFormatgetDateTimeInstance(DateFormatFULL,DateFormatFULL); //显示日期,周,时间(精确到秒)
String str5 = d5format(now);
DateFormat d6 = DateFormatgetDateTimeInstance(DateFormatLONG,DateFormatLONG); //显示日期。时间(精确到秒)
String str6 = d6format(now);
DateFormat d7 = DateFormatgetDateTimeInstance(DateFormatSHORT,DateFormatSHORT); //显示日期,时间(精确到分)
String str7 = d7format(now);
DateFormat d8 = DateFormatgetDateTimeInstance(DateFormatMEDIUM,DateFormatMEDIUM); //显示日期,时间(精确到分)
String str8 = d8format(now);//与SHORT风格相比,这种方式最好用
Systemoutprintln("用Date方式显示时间: " + now);//此方法显示的结果和CalendargetInstance()getTime()一样
Systemoutprintln("用DateFormatgetDateInstance()格式化时间后为:" + str1);
Systemoutprintln("用DateFormatgetDateTimeInstance()格式化时间后为:" + str2);
Systemoutprintln("用DateFormatgetTimeInstance()格式化时间后为:" + str3);
Systemoutprintln("用DateFormatgetInstance()格式化时间后为:" + str4);
Systemoutprintln("用DateFormatgetDateTimeInstance(DateFormatFULL,DateFormatFULL)格式化时间后为:" + str5);
Systemoutprintln("用DateFormatgetDateTimeInstance(DateFormatLONG,DateFormatLONG)格式化时间后为:" + str6);
Systemoutprintln("用DateFormatgetDateTimeInstance(DateFormatSHORT,DateFormatSHORT)格式化时间后为:" + str7);
Systemoutprintln("用DateFormatgetDateTimeInstance(DateFormatMEDIUM,DateFormatMEDIUM)格式化时间后为:" + str8);
}
}
运行结果:
用Date方式显示时间: Mon Jun 16 20:54:53 CST 2008
用DateFormatgetDateInstance()格式化时间后为:2008-6-16
用DateFormatgetDateTimeInstance()格式化时间后为:2008-6-16 20:54:53
用DateFormatgetTimeInstance()格式化时间后为:20:54:53
用DateFormatgetInstance()格式化时间后为:08-6-16 下午8:54
用DateFormatgetDateTimeInstance(DateFormatFULL,DateFormatFULL)格式化时间后为
:2008年6月16日 星期一 下午08时54分53秒 CST
用DateFormatgetDateTimeInstance(DateFormatLONG,DateFormatLONG)格式化时间后为
:2008年6月16日 下午08时54分53秒
用DateFormatgetDateTimeInstance(DateFormatSHORT,DateFormatSHORT)格式化时间后
为:08-6-16 下午8:54
用DateFormatgetDateTimeInstance(DateFormatMEDIUM,DateFormatMEDIUM)格式化时间
后为:2008-6-16 20:54:53
方法二:用javautilCalendar类来实现,看下面:
import javautil;
import javatext;
//以下是利用Calendar类来实现日期时间的,和Date类相比较比较简单
public class TestDate2 {
public static void main(String[] args) {
Calendar ca = CalendargetInstance();
int year = caget(CalendarYEAR);//获取年份
int month=caget(CalendarMONTH);//获取月份
int day=caget(CalendarDATE);//获取日
int minute=caget(CalendarMINUTE);//分
int hour=caget(CalendarHOUR);//小时
int second=caget(CalendarSECOND);//秒
int WeekOfYear = caget(CalendarDAY_OF_WEEK);
Systemoutprintln("用CalendargetInstance()getTime()方式显示时间: " + cagetTime());
Systemoutprintln("用Calendar获得日期是:" + year +"年"+ month +"月"+ day + "日");
Systemoutprintln("用Calendar获得时间是:" + hour +"时"+ minute +"分"+ second +"秒");
Systemoutprintln(WeekOfYear);//显示今天是一周的第几天(我做的这个例子正好是周二,故结果显示2,如果你再周6运行,那么显示6)
}
}
运行结果是:
用CalendargetInstance()getTime()方式显示时间: Mon Jun 16 21:54:21 CST 2008
用Calendar获得日期是:2008年5月16日
用Calendar获得时间是:9时54分21秒
2
总结:中的来说,方法二是最方便的,方法一显得分笨拙,不过看个人喜欢了。
还有一种方法利用SystemcurrentTimeMillis()也可以。
package util;
import javamathBigDecimal;
import javatextParseException;
import javatextSimpleDateFormat;
import javautilCalendar;
import javautilDate;
/
获取系统时间
/
public class DateUtil {
/ 日志对象 /
// private static Logger logger = LoggergetLogger(SystemUtilclass);
/ 获取年份 /
public static final int YEAR = 1;
/ 获取年月 /
public static final int YEARMONTH = 2;
/ 获取年月日 /
public static final int YEARMONTHDAY = 3;
/ 获取年月日,小时 /
public static final int YMD_HOUR = 4;
/ 获取年月日,小时,分钟 /
public static final int YMD_HOURMINUTE = 5;
/ 获取年月日,时分秒 /
public static final int FULL = 6;
/ 获取年月日时分秒 格式:yyyyMMddHHmmss /
public static final int UTILTIME = 7;
/
根据指定时间格式类型得到当前时间
@param type
时间类型
@return String 字符串时间
/
public static synchronized String getCurrentTime(int type) {
String format = getFormat(type);
SimpleDateFormat timeformat = new SimpleDateFormat(format);
Date date = new Date();
return timeformatformat(date);
}
/
返回当前系统时间的年月日
@return
/
public static synchronized String getCurrentTime() {
SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
return timeformatformat(date);
}
/
根据参数格式,格式化当前日期
@param format
@return
/
public static synchronized String getDateString(String format) {
SimpleDateFormat timeformat = new SimpleDateFormat(format);
Date date = new Date();
return timeformatformat(date);
}
/
根据指定时间格式类型,格式化时间格式
@param type
时间格式类型
@return
/
private static String getFormat(int type) {
String format = "";
if (type == 1) {
format = "yyyy";
} else if (type == 2) {
format = "yyyy-MM";
} else if (type == 3) {
format = "yyyy-MM-dd";
} else if (type == 4) {
format = "yyyy-MM-dd HH";
} else if (type == 5) {
format = "yyyy-MM-dd HH:mm";
} else if (type == 6) {
format = "yyyy-MM-dd HH:mm:ss";
} else if (type == 7) {
format = "yyyyMMddHHmmss";
} else {
throw new RuntimeException("日期格式参数错误");
}
return format;
}
public static int getYear(String dateString) {
SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = ddparse(dateString);
Calendar cal = CalendargetInstance();
calsetTime(date);
return calget(CalendarYEAR);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static int getMonth(String dateString) {
SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = ddparse(dateString);
Calendar cal = CalendargetInstance();
calsetTime(date);
return calget(CalendarMONTH)+1;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static int getDay(String dateString) {
SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = ddparse(dateString);
Calendar cal = CalendargetInstance();
calsetTime(date);
return calget(CalendarDAY_OF_MONTH);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Date StringToDate(String dateStr, String formatStr) {
SimpleDateFormat dd = new SimpleDateFormat(formatStr);
Date date = null;
try {
date = ddparse(dateStr);
} catch (ParseException e) {
eprintStackTrace();
}
return date;
}
/
当前日期和参数日期距离的小时数 日期格式:yyyy-MM-dd HH:mm:ss
@param date
@return
/
public static double getHours(String date) {
SimpleDateFormat timeformat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
try {
Date d = new Date();
Date d1 = timeformatparse(date);
long temp = dgetTime() - d1getTime();
double f = temp / 3600000d;
BigDecimal b = new BigDecimal(f);
double f1 = bsetScale(2, BigDecimalROUND_HALF_UP)doubleValue();
return f1;
} catch (Exception e) {
eprintStackTrace();
throw new RuntimeException(e);
}
}
public static void main(String a[]) {
try {
int aa = getYear("2012-01-08");
Systemoutprintln(aa);
} catch (Exception e) {
eprintStackTrace();
}
}
}
以上就是关于JAVA中获取系统当前时间该怎么写全部的内容,包括:JAVA中获取系统当前时间该怎么写、在JAVA中怎么获取当前时间的月份。并转换成int型、如何取得当前日期对应的季度及前面的四个季度等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)