方法如下
要使用 java
Date date = new Date();
date
還有一種方式
都是得到一個當前的時間的long型的時間的毫秒值
一
DateFormat
二
DateFormat
三
new java
一
import java
import java
public class NowString {
public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("yyyy
System
}
}
二
String sqlst = "select convert(varchar(
System
先要輸入兩個java包
import java
import java
然後
定義日期格式
sql語句為
輸出
System
java中獲取當前日期和時間的方法
代碼如下 import java
import java
import java
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
System
Calendar c = Calendar
int year = c
int month = c
int date = c
int hour = c
int minute = c
int second = c
System
}
}
有時候要把String類型的時間轉換為Date類型
SimpleDateFormat sdf=new SimpleDateFormat("yyyy
java
try {
time= sdf
} catch (ParseException e) {
e
}
一個不錯日期時間類
代碼如下 package com
import java
import java
import java
public class TestGetSystemTime {
static Date nowTime = null;
/**
* @param args
*/
public static void main(String[] args) {
// 以下為 Clendar 測試代碼
int yyyy
Calendar c = Calendar
yyyy = c
mm = c
dd = c
System
System
System
//
Timestamp ts = new Timestamp(System
String dateStr
System
//
Date date = new Date();
String dateStr
System
// 得到JAVA運行環境的一些基本屬性
System
}
}
一個完整的獲取日期實例
格式化日期 String
SimpleDateFormat sdf = new SimpleDateFormat("yyyy
Date date = sdf
String sdate = sdf
package com
import java
import java
import java
import java
import java
import java
public class TimeTest {
//用來全局控制 上一周
private int weeks =
private int MaxDate;//一月最大天數
private int MaxYear;//一年最大天數
/**
* @param args
*/
public static void main(String[] args) {
TimeTest tt = new TimeTest();
System
System
System
System
System
System
System
System
System
System
System
System
System
System
System
System
System
System
System
System
System
System
System
}
/**
* 得到指定月後(前)的日期 參數傳負數即可
*/
public static String getAfterMonth(int month) {
Calendar c = Calendar
SimpleDateFormat sdf = new SimpleDateFormat("yyyy
Date date = null;
try{
date = sdf
}catch(Exception e){
}
c
c
String strDate = sdf
return strDate;
}
/**
* 得到二個日期間的間隔天數
*/
public static String getTwoDay(String sj
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy
long day =
try {
java
java
day = (date
} catch (Exception e) {
return "";
}
return day + "";
}
/**
* 根據一個日期
*/
public static String getWeek(String sdate) {
// 再轉換為時間
Date date = TimeTest
Calendar c = Calendar
c
// int hour=c
// hour 中存的就是星期幾了
//
return new SimpleDateFormat("EEEE")
}
/**
* 將短時間格式字符串轉換為時間 yyyy
*/
public static Date strToDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy
ParsePosition pos = new ParsePosition(
Date strtodate = formatter
return strtodate;
}
/**
* 兩個時間之間的天數
*
* @param date
* @param date
* @return
*/
public static long getDays(String date
if (date
return
if (date
return
// 轉換為標准時間
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy
java
java
try {
date = myFormatter
mydate = myFormatter
} catch (Exception e) {
}
long day = (date
return day;
}
// 計算當月最後一天
public String getDefaultDay(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy
Calendar lastDate = Calendar
lastDate
lastDate
lastDate
str=sdf
return str;
}
// 上月第一天
public String getPreviousMonthFirst(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy
Calendar lastDate = Calendar
lastDate
lastDate
//lastDate
str=sdf
return str;
}
//獲取當月第一天
public String getFirstDayOfMonth(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy
Calendar lastDate = Calendar
lastDate
str=sdf
return str;
}
// 獲得本周星期日的日期
public String getCurrentWeekday() {
weeks =
int mondayPlus = this
GregorianCalendar currentDate = new GregorianCalendar();
currentDate
Date monday = currentDate
DateFormat df = DateFormat
String preMonday = df
return preMonday;
}
//獲取當天時間
public String getNowTime(String dateformat){
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);//可
以方便
地修改日期格式
String hehe = dateFormat
return hehe;
}
// 獲得當前日期與本周日相差的天數
private int getMondayPlus() {
Calendar cd = Calendar
// 獲得今天是一周的第幾天
int dayOfWeek = cd
作為第一
天所以這裡減
if (dayOfWeek ==
return
} else {
return
}
}
//獲得本周一的日期
public String getMondayOFWeek(){
weeks =
int mondayPlus = this
GregorianCalendar currentDate = new GregorianCalendar();
currentDate
Date monday = currentDate
DateFormat df = DateFormat
String preMonday = df
return preMonday;
}
//獲得相應周的周六的日期
public String getSaturday() {
int mondayPlus = this
GregorianCalendar currentDate = new GregorianCalendar();
currentDate
Date monday = currentDate
DateFormat df = DateFormat
String preMonday = df
return preMonday;
}
// 獲得上周星期日的日期
public String getPreviousWeekSunday() {
weeks=
weeks
int mondayPlus = this
GregorianCalendar currentDate = new GregorianCalendar();
currentDate
Date monday = currentDate
DateFormat df = DateFormat
String preMonday = df
return preMonday;
}
// 獲得上周星期一的日期
public String getPreviousWeekday() {
weeks
int mondayPlus = this
GregorianCalendar currentDate = new GregorianCalendar();
currentDate
Date monday = currentDate
DateFormat df = DateFormat
String preMonday = df
return preMonday;
}
// 獲得下周星期一的日期
public String getNextMonday() {
weeks++;
int mondayPlus = this
GregorianCalendar currentDate = new GregorianCalendar();
currentDate
Date monday = currentDate
DateFormat df = DateFormat
String preMonday = df
return preMonday;
}
// 獲得下周星期日的日期
public String getNextSunday() {
int mondayPlus = this
GregorianCalendar currentDate = new GregorianCalendar();
currentDate
Date monday = currentDate
DateFormat df = DateFormat
String preMonday = df
return preMonday;
}
private int getMonthPlus(){
Calendar cd = Calendar
int monthOfNumber = cd
cd
cd
MaxDate=cd
if(monthOfNumber ==
return
}else{
return
}
}
//獲得上月最後一天的日期
public String getPreviousMonthEnd(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy
Calendar lastDate = Calendar
lastDate
lastDate
lastDate
str=sdf
return str;
}
//獲得下個月第一天的日期
public String getNextMonthFirst(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy
Calendar lastDate = Calendar
lastDate
lastDate
str=sdf
return str;
}
//獲得下個月最後一天的日期
public String getNextMonthEnd(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy
Calendar lastDate = Calendar
lastDate
lastDate
lastDate
str=sdf
return str;
}
//獲得明年最後一天的日期
public String getNextYearEnd(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy
Calendar lastDate = Calendar
lastDate
lastDate
lastDate
str=sdf
return str;
}
//獲得明年第一天的日期
public String getNextYearFirst(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy
Calendar lastDate = Calendar
lastDate
lastDate
str=sdf
return str;
}
//獲得本年有多少天
private int getMaxYear(){
Calendar cd = Calendar
cd
cd
int MaxYear = cd
return MaxYear;
}
private int getYearPlus(){
Calendar cd = Calendar
int yearOfNumber = cd
cd
cd
int MaxYear = cd
if(yearOfNumber ==
return
}else{
return
}
}
//獲得本年第一天的日期
public String getCurrentYearFirst(){
int yearPlus = this
GregorianCalendar currentDate = new GregorianCalendar();
currentDate
Date yearDay = currentDate
DateFormat df = DateFormat
String preYearDay = df
return preYearDay;
}
//獲得本年最後一天的日期 *
public String getCurrentYearEnd(){
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方
便地修
改日期格式
String years = dateFormat
return years+"
}
//獲得上年第一天的日期 *
public String getPreviousYearFirst(){
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方
便地修
改日期格式
String years = dateFormat
(years);
years_value
return years_value+"
}
//獲得上年最後一天的日期
public String getPreviousYearEnd(){
weeks
int yearPlus = this
GregorianCalendar currentDate = new GregorianCalendar();
currentDate
Date yearDay = currentDate
DateFormat df = DateFormat
String preYearDay = df
getThisSeasonTime(
return preYearDay;
}
//獲得本季度
public String getThisSeasonTime(int month){
int array[][] = {{
int season =
if(month>=
season =
}
if(month>=
season =
}
if(month>=
season =
}
if(month>=
season =
}
int start_month = array[season
int end_month = array[season
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方
便地修
改日期格式
String years = dateFormat
int years_value = Integer
int start_days =
";//getLastDayOfMonth(years_value
int end_days = getLastDayOfMonth(years_value
String seasonDate =
years_value+"
return seasonDate;
}
/**
* 獲取某年某月的最後一天
* @param year 年
* @param month 月
* @return 最後一天
*/
private int getLastDayOfMonth(int year
if (month ==
|| month ==
return
}
if (month ==
return
}
if (month ==
if (isLeapYear(year)) {
return
} else {
return
}
}
return
}
/**
* 是否閏年
* @param year 年
* @return
*/
public boolean isLeapYear(int year) {
return (year %
}
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19983.html