本文目錄一覽:
- 1、java 時間計算
- 2、java編程中,如何實現時間的計算?
- 3、java整么計算時間
- 4、java時間加減
- 5、java計算時間
java 時間計算
自己寫的一個,使用了兩種方法實現了你的需求,希望可以幫到你
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TimeTest {
/**
* @param args
*/
public static void main(String[] args) {
TimeTest t = new TimeTest();
SimpleDateFormat formater = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”);
String currentTime = formater.format(new Date());
System.out.println(“當前日期:”+currentTime);
System.out.println(“昨日日期:”+t.getYtime(currentTime));
System.out.println(“上月日期:”+t.getLtime(currentTime));
}
// 昨天
public String getYtime(String today){
String ytime = “”;
Date date = new Date(today);
date = new Date(date.getTime() – 1000 * 60 * 60 * 24);
ytime = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”).format(date);
return ytime;
}
// 上月
public String getLtime(String today){
String ltime = “”;
Date date = new Date(today);
Calendar cal= Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, -1);
cal.add(Calendar.MONTH, -1);
date = cal.getTime();
ltime = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”).format(date);
return ltime;
}
}
java編程中,如何實現時間的計算?
得到當前的時間 Date date = new Date();
如果要計算 2個時間的差 比如 2012-12-21 離 現在還有多少天 ?
long a = date.getTime();
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
long b = sdf.parse(“2012-12-21”).getTime();
int success = (int) ((b-a)/(1000*60*60*24)); //1000毫秒*60分鐘*60秒*24小時 = 天
System.out.println(“距離2012-12-21還有”+success+”天”);
java整么計算時間
提供一個時間操作類給你,你可以調用裡面的dateDiff方法,dateDiff(new Date(), getDateFromString(“明天的字元串”)表示的就是現在到明天0點之前的差距天數。
你要分鐘數的話就自己改一下dateDiff,不要除以60和24就是分鐘了
以下是工具類代碼
import org.apache.log4j.Logger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class AdDateUtil {
private static Logger logger = Logger.getLogger(AdDateUtil.class);
static public String getNowStr(String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
String now = sdf.format(new Date());
return now;
}
static public Date getFormatDate(String date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date d = new Date();
try {
d = sdf.parse(date);
} catch (ParseException e) {
logger.error(e);
}
return d;
}
static public String getDateStr(Date date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
String d = sdf.format(date);
return d;
}
static public String getPadZeroString(String s, int size) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i (size – s.length()); i++) {
sb.append(“0”);
}
sb.append(s);
return sb.toString();
}
/**
* 得到某月的天數
*
* @param year
* @param month
* @return
*/
static public int getDayCountOfMonth(String year, String month) {
Calendar cal = Calendar.getInstance();
// 年
cal.set(Calendar.YEAR, Integer.parseInt(year));
// 月,因為Calendar里的月是從0開始,所以要-1
cal.set(Calendar.MONTH, Integer.parseInt(month) – 1);
return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}
static public String getYesterday(String format) {
SimpleDateFormat df = new SimpleDateFormat(format);
Calendar now = Calendar.getInstance();
now.roll(Calendar.DAY_OF_YEAR, -1); //昨天
return df.format(now.getTime());
}
/**
* 獲取和今天附近的某天
* @param format
* @param diff
* @return
*/
static public String getADay(String format, int diff) {
SimpleDateFormat df = new SimpleDateFormat(format);
Calendar now = Calendar.getInstance();
int beforeM = now.get(Calendar.MONTH);
now.roll(Calendar.DAY_OF_YEAR, diff); //
int nowM = now.get(Calendar.MONTH);
//必須進行日期處理,否則2009-01-04日前七天是2009-12-28
if (nowM beforeM) {
now.roll(Calendar.YEAR, -1);
}
return df.format(now.getTime());
}
static public String getTomorrow(String format) {
SimpleDateFormat df = new SimpleDateFormat(format);
Calendar now = Calendar.getInstance();
now.roll(Calendar.DAY_OF_YEAR, 1); //明天
return df.format(now.getTime());
}
/**
* 得到最近num天的全部日期
* 說明:
* 1.日期是從昨天開始算的.
* 2.如果num=2 , 日期是2008-03-14 ,則返回的結果為 2008-03-12、2008-03-13
* @param num
* @return
*/
public static String[] getDaysByNum(int num, String date) {
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
String[] result = { };
Calendar cal = Calendar.getInstance();
cal.setTime(getDateFromString(date, “yyyy-MM-dd”));
//最近一周
result = new String[num];
for (int i = num; i 0; i–) {
cal.add(Calendar.DAY_OF_YEAR, -1);
result[i – 1] = sdf.format(new Date(cal.getTimeInMillis()));
}
return result;
}
public static Date getDateFromString(String dateStr, String format) {
if ((dateStr == null) || (format == null)) {
try {
throw new Exception(“數據類型異常” + dateStr + “|” + format);
} catch (Exception e) {
logger.error(“數據類型異常:” + e);
}
}
SimpleDateFormat df = new SimpleDateFormat(format);
Date date;
try {
date = df.parse(dateStr);
return date;
} catch (Exception ex) {
logger.error(ex);
return new Date();
}
}
static public int getNowYear() {
Calendar cal = Calendar.getInstance();
return cal.get(Calendar.YEAR);
}
static public int getNowMonth() {
Calendar cal = Calendar.getInstance();
return cal.get(Calendar.MONTH) + 1;
}
public static String[] getMonthRang(String year, String month) {
String beginDate = year + “-” + month + “-01”;
String endDate = year + “-” + month + “-” +
getDayCountOfMonth(year, month);
return getDaysByRang(beginDate, endDate);
}
public static String[] getDaysByRang(String beginDate, String endDate) {
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
//得到兩個日期間相差多少天
int num = dateDiff(beginDate, endDate);
if (num 0) {
//顛倒一下日期
String tmp = beginDate;
beginDate = endDate;
endDate = tmp;
num = 0 – num;
}
String[] result = { };
Calendar cal = Calendar.getInstance();
try {
cal.setTime(sdf.parse(beginDate));
} catch (ParseException e) {
e.printStackTrace();
}
num = num + 1; //把開始和結束日期都包含進去
result = new String[num];
for (int i = 0; i num; i++) {
if (i 0) {
cal.add(Calendar.DAY_OF_YEAR, 1);
}
result[i] = sdf.format(new Date(cal.getTimeInMillis()));
}
return result;
}
public static int dateDiff(String beginDate, String endDate) {
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
Date date = null;
try {
date = sdf.parse(endDate);
} catch (ParseException e) {
date = new Date();
e.printStackTrace();
}
long end = date.getTime();
try {
date = sdf.parse(beginDate);
} catch (ParseException e) {
date = new Date();
e.printStackTrace();
}
long begin = date.getTime();
long day = (end – begin) / (1000 * 3600 * 24); //除1000是把毫秒變成秒
return Integer.parseInt(Long.toString(day));
}
public static void main(String[] args) {
System.out.println(AdDateUtil.getADay(“yyyy-MM-dd”, -7));
}
}
java時間加減
附上代碼:
Date date = new Date(“2014/1/10 18:20”);
Date date2 = new Date(“2014/1/11 3:5”);
long temp = date2.getTime() – date.getTime(); //相差毫秒數
long hours = temp / 1000 / 3600; //相差小時數
long temp2 = temp % (1000 * 3600);
long mins = temp2 / 1000 / 60; //相差分鐘數
System.out.println(“date2 與 date 相差” + hours + “小時”+ mins + “分鐘”);
****************************************希望能夠幫助到你!************************************************
如果我的回答對你有幫助,
別忘了點擊我的回答下方【選為滿意答案】按鈕。
謝謝!
java計算時間
可以這麼寫:
public static void compute() {
Scanner scanner = new Scanner(System.in);
int time1, time2, hours, minutes;
time1 = scanner.nextInt();
time2 = scanner.nextInt();
String t1 = String.valueOf(time1);
String t2 = String.valueOf(time2);
//開始時間的小時
int t1_hour = Integer.parseInt(t1.substring(0, t1.length()-2));
//結束時間的小時
int t2_hour = Integer.parseInt(t2.substring(0, t2.length() – 2));
//開始時間的分鐘
int t1_minute = Integer.parseInt(t1.substring(t1.length()-2));
//結束時間的分鐘
int t2_minute = Integer.parseInt(t2.substring(t2.length() – 2));
//時間差的小時
hours =((t2_hour * 60 + t2_minute)- (t1_hour * 60 + t1_minute))/60;
//時間差的分鐘
minutes =((t2_hour * 60 + t2_minute)- (t1_hour * 60 + t1_minute))%60;
System.out.println(“The train journey time is “+hours+” hrs “+ minutes+” mins.”);
scanner.close();
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/152962.html