首页 > 代码库 > java 显示当前的时间
java 显示当前的时间
JAVA中获取当前系统时间 java中获取当前时间一. 获取当前系统时间和日期并格式化输出:
import java.util.Date;
import java.text.SimpleDateFormat;
public class NowString {
public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
}
}
二. 在数据库里的日期只以年-月-日的方式输出,可以用下面两种方法:
1、用convert()转化函数:
String sqlst = "select convert(varchar(10),bookDate,126) as convertBookDate from roomBook where bookDate between ‘2007-4-10‘ and ‘2007-4-25‘";
System.out.println(rs.getString("convertBookDate"));
2、利用SimpleDateFormat类:
先要输入两个java包:
import java.util.Date;
import java.text.SimpleDateFormat;
然后:
定义日期格式:SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd);
sql语句为:String sqlStr = "select bookDate from roomBook where bookDate between ‘2007-4-10‘ and ‘2007-4-25‘";
输出:
System.out.println(df.format(rs.getDate("bookDate")));
************************************************************
java中获取当前日期和时间的方法
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
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 = dateFormat.format( now );
System.out.println(hehe);
Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int date = c.get(Calendar.DATE);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second);
}
}
public class TestGetTime
{
public static String getDateTimeString()
{
java.text.SimpleDateFormat
simpleDateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return simpleDateFormat.format(new java.util.Date(System.currentTimeMillis()));
}
public static void main(String[] args)
{
System.out.println(getDateTimeString());
}
}
GregorianCalender (标准日历类)
GregorianCalender(int year,int month,int datOfMonth)
GregorianCalender(int year,int month,int datOfMonth,int hours,int minutes)
GregorianCalender(int year,int month,int datOfMonth,int hours,int minutes,int seconds)
三种形式中,都设置了日,月和年。 year指定了从1900年起的年数。month指定了月,以0表示一月。月中的日由dayOfMonth指定。第一种形式以午夜设置时间。第二种形式以小时和分钟设置,第三种形式增加了秒
Calender(日历类)
import java.util.Calendar;
class CalendarDemo
{
public static void main(String[] args)
{
String months[] = {"jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
Calendar calendar = Calendar.getInstance();
System.out.print("Date: ");
System.out.print(months[calendar.get(Calendar.MONTH)]);
System.out.print(" "+calendar.get(Calendar.DATE)+" ");
System.out.println(calendar.get(Calendar.YEAR));
System.out.print("Time: ");
System.out.print(calendar.get(Calendar.HOUR)+":");
System.out.print(calendar.get(Calendar.MINUTE)+":");
System.out.println(calendar.get(Calendar.SECOND));
System.out.print("Updated time: ");
System.out.print(calendar.get(Calendar.HOUR)+":");
System.out.print(calendar.get(Calendar.MINUTE)+":");
System.out.println(calendar.get(Calendar.SECOND));
}
}
下面是令一种实现的方式,使用java.text.SimpleDateFormat.
并附带测试样例:
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
public final class DateUtils {
private static final SimpleDateFormat YEAR_FORMAT = new SimpleDateFormat("yyyy");
private static final SimpleDateFormat MONTH_FORMAT = new SimpleDateFormat("MM");
private static final SimpleDateFormat DAY_FORMAT = new SimpleDateFormat("dd");
private static final SimpleDateFormat HOUR_FORMAT = new SimpleDateFormat("HH");
private static final SimpleDateFormat MINUTE_FORMAT = new SimpleDateFormat("mm");
private static final SimpleDateFormat SECOND_FORMAT = new SimpleDateFormat("ss");
public static String formatYear(Date date) {
return date==null?null:YEAR_FORMAT.format(date);
}
public static String formatMonth(Date date) {
return date==null?null:MONTH_FORMAT.format(date);
}
public static String formatDay(Date date) {
return date==null?null:DAY_FORMAT.format(date);
}
public static String formatHour(Date date) {
return date==null?null:HOUR_FORMAT.format(date);
}
public static String formatMinute(Date date) {
return date==null?null:MINUTE_FORMAT.format(date);
}
public static String formatSecond(Date date) {
return date==null?null:SECOND_FORMAT.format(date);
}
// for unit test below
private static final SimpleDateFormat NORMAL_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
static Date parse(String dateString) throws ParseException {
return dateString==null||dateString.length()==0?null:NORMAL_FORMAT.parse(dateString);
}
}
-----------------------------------------------
单元测试:
import junit.framework.TestCase;
import java.util.Date;
public class TestDateUtils extends TestCase {
public TestDateUtils(String name) {super(name);}
public void testFormatDate() throws Exception {
String testDate = "2003-03-12 15:34:54";
Date d = DateUtils.parse(testDate);
assertNotNull(d);
assertEquals("2003", DateUtils.formatYear(d));
assertEquals("03", DateUtils.formatMonth(d));
assertEquals("12", DateUtils.formatDay(d));
assertEquals("15", DateUtils.formatHour(d));
assertEquals("34", DateUtils.formatMinute(d));
assertEquals("54", DateUtils.formatSecond(d));
}
}
System.out.println("ERA: " + calendar.get(Calendar.ERA));
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: "
+ calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
System.out.println("ZONE_OFFSET: "
+ (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
System.out.println("DST_OFFSET: "
+ (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));
System.out.println("Current Time, with hour reset to 3");
calendar.clear(Calendar.HOUR_OF_DAY); // so doesn‘t override
calendar.set(Calendar.HOUR, 3);
Java中获得当前时间的方法
有两种方法:
方法一:用java.util.Date类来实现,并结合java.text.DateFormat类来实现时间的格式化,看下面代码:
import java.util.*;
import java.text.*;
//以下默认时间日期显示方式都是汉语语言方式
//一般语言就默认汉语就可以了,时间日期的格式默认为MEDIUM风格,比如:2008-6-16 20:54:53
//以下显示的日期时间都是再Date类的基础上的来的,还可以利用Calendar类来实现见类TestDate2.java
public class TestDate {
public static void main(String[] args) {
Date now = new Date();
Calendar cal = Calendar.getInstance();
DateFormat d1 = DateFormat.getDateInstance(); //默认语言(汉语)下的默认风格(MEDIUM风格,比如:2008-6-16 20:54:53)
String str1 = d1.format(now);
DateFormat d2 = DateFormat.getDateTimeInstance();
String str2 = d2.format(now);
DateFormat d3 = DateFormat.getTimeInstance();
String str3 = d3.format(now);
DateFormat d4 = DateFormat.getInstance(); //使用SHORT风格显示日期和时间
String str4 = d4.format(now);
DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //显示日期,周,时间(精确到秒)
String str5 = d5.format(now);
DateFormat d6 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //显示日期。时间(精确到秒)
String str6 = d6.format(now);
DateFormat d7 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //显示日期,时间(精确到分)
String str7 = d7.format(now);
DateFormat d8 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //显示日期,时间(精确到分)
String str8 = d8.format(now);//与SHORT风格相比,这种方式最好用
System.out.println("用Date方式显示时间: " + now);//此方法显示的结果和Calendar.getInstance().getTime()一样
System.out.println("用DateFormat.getDateInstance()格式化时间后为:" + str1);
2 Java中获得当前时间的方法
System.out.println("用DateFormat.getDateTimeInstance()格式化时间后为:" + str2);
System.out.println("用DateFormat.getTimeInstance()格式化时间后为:" + str3);
System.out.println("用DateFormat.getInstance()格式化时间后为:" + str4);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为:" + str5);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为:" + str6);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后为:" + str7);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间后为:" + str8);
}
}
运行结果:
用Date方式显示时间: Mon Jun 16 20:54:53 CST 2008
用DateFormat.getDateInstance()格式化时间后为:2008-6-16
用DateFormat.getDateTimeInstance()格式化时间后为:2008-6-16 20:54:53
用DateFormat.getTimeInstance()格式化时间后为:20:54:53
用DateFormat.getInstance()格式化时间后为:08-6-16 下午8:54
用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为
:2008年6月16日 星期一 下午08时54分53秒 CST
用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为
:2008年6月16日 下午08时54分53秒
用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后
为:08-6-16 下午8:54
用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间
后为:2008-6-16 20:54:53
方法二:用java.util.Calendar类来实现,看下面:
import java.util.*;
import java.text.*;
//以下是利用Calendar类来实现日期时间的,和Date类相比较比较简单
public class TestDate2 {
public static void main(String[] args) {
Calendar ca = Calendar.getInstance();
int year = ca.get(Calendar.YEAR);//获取年份
int month=ca.get(Calendar.MONTH);//获取月份
int day=ca.get(Calendar.DATE);//获取日
int minute=ca.get(Calendar.MINUTE);//分
int hour=ca.get(Calendar.HOUR);//小时
int second=ca.get(Calendar.SECOND);//秒
int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK);
System.out.println("用Calendar.getInstance().getTime()方式显示时间: " + ca.getTime());
System.out.println("用Calendar获得日期是:" + year +"年"+ month +"月"+ day + "日");
System.out.println("用Calendar获得时间是:" + hour +"时"+ minute +"分"+ second +"秒");
System.out.println(WeekOfYear);//显示今天是一周的第几天(我做的这个例子正好是周二,故结果显示2,如果你再周6运行,那么显示6)
}
}
运行结果是:
用Calendar.getInstance().getTime()方式显示时间: Mon Jun 16 21:54:21 CST 2008
用Calendar获得日期是:2008年5月16日
用Calendar获得时间是:9时54分21秒
2
总结:中的来说,方法二是最方便的,方法一显得分笨拙,不过看个人喜欢了。
还有一种方法利用System.currentTimeMillis()也可以的,下次再总结这种方法。
java 显示当前的时间