本文目錄一覽:
- 1、在Java中如何輸出當前系統日期?
- 2、Java中如何獲取當前時間。
- 3、java 獲取當前日期,應該如何操作呢
- 4、如何用java語言 獲得系統當前日期
- 5、java如何獲取當前時間 年月日 時分秒
- 6、java裡面有沒有直接獲取當前日期的方法
在Java中如何輸出當前系統日期?
利用java里的Date類輸出,進階的做法還可以用simpleDateformat類進行格式化輸出日期。代碼如下:
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 日期格式化
* @author young
*
*/
public class SimpleDateFormatTest {
public static void main(String[] args) {
// 在構造器中傳入日期樣式
// SimpleDateFormat sdf=new SimpleDateFormat(
// “yyyy.MM.dd G ‘at’ HH:mm:ss z”);
SimpleDateFormat sdf = new SimpleDateFormat(“dd/MM/yyyy”);
// sdf=new SimpleDateFormat(“yyyy年MM月dd日 HH時mm分ss秒”);
// 當前系統時間
Date date = new Date();
// 調用format(Date date)對象傳入的日期參數進行格式化
// format(Date date)將日期轉化成字元串
String formatDate = sdf.format(date);
System.out.println(“格式化後的日期為:” + formatDate);
}
}
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);
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秒
java 獲取當前日期,應該如何操作呢
package util;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 獲取系統時間
*
*/
public class DateUtil {
/* 日誌對象 */
// private static Logger logger = Logger.getLogger(SystemUtil.class);
/* 獲取年份 */
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 timeformat.format(date);
}
/**
* 返回當前系統時間的年月日
*
* @return
*/
public static synchronized String getCurrentTime() {
SimpleDateFormat timeformat = new SimpleDateFormat(“yyyy-MM-dd”);
Date date = new Date();
return timeformat.format(date);
}
/**
* 根據參數格式,格式化當前日期
* @param format
* @return
*/
public static synchronized String getDateString(String format) {
SimpleDateFormat timeformat = new SimpleDateFormat(format);
Date date = new Date();
return timeformat.format(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 = dd.parse(dateString);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.YEAR);
} 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 = dd.parse(dateString);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.MONTH)+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 = dd.parse(dateString);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.DAY_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 = dd.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
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 = timeformat.parse(date);
long temp = d.getTime() – d1.getTime();
double f = temp / 3600000d;
BigDecimal b = new BigDecimal(f);
double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
return f1;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static void main(String a[]) {
try {
int aa = getYear(“2012-01-08”);
System.out.println(aa);
} catch (Exception e) {
e.printStackTrace();
}
}
}
如何用java語言 獲得系統當前日期
java語言 獲得系統當前日期:
1、Date date=new Date();這個是java提供的時間類,可以從中取出,年、月日、時、分、秒
2、SimpleDateFormat這個是時間格式類,對時間進行格式化
String time=new SimpleDateFormat(“HH:mm:ss”).format(new Date())
time=15:02:03
String time=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”).format(new Date())
time=2015-05-26 15:02:03
3、System.currentTimeMillis(),返回的是long型日期時間
long time=System.currentTimeMillis();
time=352632563256;
java如何獲取當前時間 年月日 時分秒
java如何獲取當前時間以及格式化需要用到兩個類,如下圖:
1.獲取當前時間,並格式化為(年-月-日 時:分:秒)。
Date t = new Date();
SimpleDateFormat df = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
System.out.println(df.format(t));
列印輸出結果如下圖:
2.將java.util.Date轉換為java.sql.Date格式。
java.sql.Date sqld = new java.sql.Date(t.getTime());
System.out.println(sqld);
java.sql.Time sqlt = new java.sql.Time(t.getTime());
System.out.println(sqlt);
java.sql.Timestamp sqlts = new java.sql.Timestamp(t.getTime());
System.out.println(sqlts);
列印輸出結果如下圖:
「拓展資料——java」:
Java是一種廣泛使用的計算機編程語言,擁有跨平台、面向對象、泛型編程的特性,廣泛應用於企業級Web應用開發和移動應用開發。
Java編程語言的風格十分接近C++語言。繼承了C++語言面向對象技術的核心,捨棄了容易引起錯誤的指針,以引用取代;移除了C++中的運算符重載和多重繼承特性,用介面取代;增加垃圾回收器功能。
Java編程語言是個簡單、面向對象、分散式、解釋性、健壯、安全與系統無關、可移植、高性能、多線程和動態的語言。
java裡面有沒有直接獲取當前日期的方法
java里沒有一種方法是直接寫這種格式化的,都要通過SimpleDateFormat()方法進行轉換,可以通過new Date()方法和Calendar.getInstance().getTime()方法獲得時間,格式如下”Fri Sep 30 16:38:28 CST 2011″ 。所有獲得時間都要通過SimpleDateFormat()方法轉換才會是「2012-05-12 14:28:55」這個樣子。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/288680.html