本文目錄一覽:
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 獲取當前日期,應該如何操作呢
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中獲取系統當前時間該怎麼寫?
一. 獲取當前系統時間和日期並格式化輸出:\x0d\x0a\x0d\x0aimport java.util.Date; \x0d\x0aimport java.text.SimpleDateFormat;\x0d\x0a\x0d\x0apublic class NowString { \x0d\x0a public static void main(String[] args) { \x0d\x0a SimpleDateFormat df = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);//設置日期格式\x0d\x0a System.out.println(df.format(new Date()));// new Date()為獲取當前系統時間\x0d\x0a } \x0d\x0a} \x0d\x0a\x0d\x0a二. 在資料庫里的日期只以年-月-日的方式輸出,可以用下面兩種方法:\x0d\x0a\x0d\x0a1、用convert()轉化函數:\x0d\x0a\x0d\x0aString sqlst = “select convert(varchar(10),bookDate,126) as convertBookDate from roomBook where bookDate between ‘2007-4-10’ and ‘2007-4-25′”;\x0d\x0a\x0d\x0aSystem.out.println(rs.getString(“convertBookDate”)); \x0d\x0a\x0d\x0a2、利用SimpleDateFormat類:\x0d\x0a\x0d\x0a先要輸入兩個java包:\x0d\x0a\x0d\x0aimport java.util.Date; \x0d\x0aimport java.text.SimpleDateFormat;\x0d\x0a\x0d\x0a然後:\x0d\x0a\x0d\x0a定義日期格式:SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd);\x0d\x0a\x0d\x0asql語句為:String sqlStr = “select bookDate from roomBook where bookDate between ‘2007-4-10’ and ‘2007-4-25′”;\x0d\x0a\x0d\x0a輸出:\x0d\x0a\x0d\x0aSystem.out.println(df.format(rs.getDate(“bookDate”)));
java怎麼獲取當前日期
java中的Date類,實例化即可獲取當前日期:
(import java.util.Date;
import java.text.SimpleDateFormat; )
代碼:
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy/MM/dd”);
String curDate= dateFormat.format( now );
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/253316.html