利用類對象計算日期
在利用Java語言進行信息系統開發中,經常需要對日期進行計算和轉換,比如,設置了某活動的開始日期和結束日期,系統需要判斷當前是否是該活動時間,在Java開發的信息系統中,通常日期以字符串形式“yyyy-MM-dd hh:mm:ss”保存到數據庫中,在進行轉換中,通常需要將字符串表示的日期轉換為Date對象,Java API提供了一個工具SimpleDateFormat對象能將一個滿足“yyyy-MM-dd hh:mm:ss”形式的字符串轉換成一個Date對象。在日期轉換中,經常也利用到時間戳,什么是時間戳(timestamp)?時間戳(timestamp)是一種時間表示方式,定義為從格林威治時間1970年01月01日00時00分00秒起至現在的總秒數。通過日期對象的getTime()將獲得該日期的當前時間戳的毫米數。
將日期字符串轉換為Date對象的參考代碼如下:
try {
String dateString = "2009-08-02 13:43:00";
DateFormat df = SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = df.parse(dateString);
} catch(Exception ex) {
}
給你的問題是,如何給出某活動的開始日期和結束日期,已經當前日期的時間戳,需要你編程判定當前日期所處的位置.
樣例輸入:
輸入的第一行為已個正整數N,表示需要你計算活動次數,接下來的N行,每行的第一個數為一個10位正整數,表示當前日期的時間戳秒數,接下來是兩個字符串,表示活動開始日期和結束日期。字符串滿足日期“yyyy-MM-dd hh:mm:ss”格式。
樣例輸出:
如果當前日期在活動開始之前,就輸出“Before",如果在活動之中輸出"NOW", 如果在活動之后,輸出"After".
測試輸入數據:
2
1389339888 "2013-10-11 12:12:12" "2013-10-12 12:12:12"
1389339888 "2014-01-09 12:12:12" "2014-01-12 12:12:12"
測試輸出數據:
After
NOW
實訓分析:本問題是多測試用例問題,用例數可知,解決一個用例的類型為:
類型名稱:DateQuestion
成員變量:
private long tms;
private Data t_begin,t_end;
可用不同的構造方法完成輸入的行提取tms和t_begin,t_end。
構造方法1:
public DateQuestion (String s){
//完成輸入的行提取tms和t_begin,t_end;
}
構造方法2:
public DateQuestion (long t,String s){
//完成輸入的行提取tms和t_begin,t_end;
}
構造方法3:
public DateQuestion (long t,String s1,String s2){
//完成輸入的行提取tms和t_begin,t_end;
}
public String getResult(){
//從日期中提取毫秒數(date.getTime()方法),再與tms的大小進行比較,算法比較簡單
}
提示:本實訓的難度在於從輸入行中提取日期數據,方法有多種,建議先讀取一個long數據,再讀一行,從行字符串中使用”將字符串分成多組,分組方法使用Scanner對象.
方法一:
構造方法public DateQuestion (String s){ }
import java.util.Date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Scanner; class DateQuestion { private long tms;// 時間戳秒數 private Date t_begin,t_end; // 開始日期和結束日期 public DateQuestion(String s) { // 完成輸入的行提取tms和t_begin,t_end; } tms = Long.parseLong(s.substring(0, 10)); SimpleDateFormat dat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); try { t_begin = dat.parse(s.substring(12, 31)); t_end = dat.parse(s.substring(34, s.length() - 1)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String getResult() { // 從日期中提取毫秒數(date.getTime()方法),再與tms的大小進行比較,算法比較簡單 Long time_me = tms * 1000; long time_begin = t_begin.getTime(); long time_end = t_end.getTime(); String result; if (time_me < time_begin) result = "Before"; else if (time_me > time_end) result = "After"; else result = "NOW"; return result; } } public class DateTest { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); String s = in.nextLine(); for (int i = 0; i < n; i++) { s = in.nextLine(); DateQuestion date = new DateQuestion(s); System.out.println(date.getResult()); } } }
方法二:構造方法public DateQuestion (long t ,String s){ }
class DateQuestion { private long tms;// 時間戳秒數 private Date t_begin,Date t_end; // 開始日期和結束日期 public DateQuestion(long t,String s) { // 完成輸入的行提取tms和t_begin,t_end; String ss[]=s.split("\""); tms = t; try { SimpleDateFormat dat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); t_begin = dat.parse(ss[1]); t_end = dat.parse(ss[3]); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String getResult() { // 從日期中提取毫秒數(date.getTime()方法),再與tms的大小進行比較,算法比較簡單 Long time_me = tms * 1000; long time_begin = t_begin.getTime(); long time_end = t_end.getTime(); String result; if (time_me < time_begin) result = "Before"; else if (time_me > time_end) result = "After"; else result = "NOW"; return result; } } public class DateTest { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); String s = in.nextLine(); for (int i = 0; i < n; i++) { long t=in.nextLong(); s = in.nextLine(); DateQuestion date = new DateQuestion(t,s); System.out.println(date.getResult()); } } }
方法三:
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Scanner; public class p12 { public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); int n = in.nextInt(); String s = in.nextLine(); SimpleDateFormat dat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); for (int k = 0; k < n; k++) { String ss[] = new String[3]; s=in.nextLine(); ss[0] = s.substring(0, 10); ss[1] = s.substring(12, 31); ss[2] = s.substring(34, s.length() -1); Long time_me = Long.parseLong(ss[0]) * 1000; long time_begin = dat.parse(ss[1]).getTime(); long time_end = dat.parse(ss[2]).getTime(); if (time_me < time_begin) { System.out.println("Before"); } else if (time_me > time_end) { System.out.println("After"); } else { System.out.println("NOW"); } } } }