JAVA-日期类(Date、SimpleDateFormat)
目錄
1、Date類
2、Calendar類
java.util.Date日期類
java.text.SimpleDateFormat日期轉(zhuǎn)換類
?SimpleDateFormat:實現(xiàn)String和Date之間的轉(zhuǎn)換
1、 日期轉(zhuǎn)字符串
import java.io.*; import java.math.BigInteger; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors;public class Main {static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));static int N = (int)1e4 + 10;static math math_bag = new math();static int fa[] = new int[N];public static void main(String[] args ) throws IOException, ParseException{// 創(chuàng)建日期對象(日期、時間)Date d = new Date();// 創(chuàng)建日期格式對象SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");// 日期轉(zhuǎn)換為字符串String str = s.format(d);pw.println(str);pw.flush();} }class rd {static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));static StringTokenizer tokenizer = new StringTokenizer("");static String nextLine() throws IOException { return reader.readLine(); }static String next() throws IOException{while(!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());return tokenizer.nextToken();}static int nextInt() throws IOException { return Integer.parseInt(next()); }static double nextDouble() throws IOException { return Double.parseDouble(next()); }static long nextLong() throws IOException { return Long.parseLong(next()); }static BigInteger nextBigInteger() throws IOException{BigInteger d = new BigInteger(rd.nextLine());return d;} }class math {int gcd(int a,int b){if(b == 0) return a;else return gcd(b,a % b);}int lcm(int a,int b){return a * b / gcd(a, b);}// 求n的所有約數(shù)List get_factor(int n){List<Long> a = new ArrayList<>();for(long i = 1; i <= Math.sqrt(n) ; i ++){if(n % i == 0){a.add(i);if(i != n / i) a.add(n / i); // // 避免一下的情況:x = 16時,i = 4 ,x / i = 4的情況,這樣會加入兩種情況 ^-^復(fù)雜度能減少多少是多少}}// 相同因子去重,這個方法,完美a = a.stream().distinct().collect(Collectors.toList());// 對因子排序(升序)Collections.sort(a);return a;}// 判斷是否是質(zhì)數(shù)boolean check_isPrime(int n){if(n < 2) return false;for(int i = 2 ; i <= n / i; i ++) if (n % i == 0) return false;return true;} }class PII implements Comparable<PII> {int x,y;public PII(int x ,int y){this.x = x;this.y = y;}public int compareTo(PII a){if(this.x-a.x != 0)return this.x-a.x; //按x升序排序else return this.y-a.y; //如果x相同,按y升序排序} }class Edge {int a,b,c;public Edge(int a ,int b, int c){this.a = a;this.b = b;this.c = c;} }2、字符串轉(zhuǎn)換為日期(用的少)
import java.io.*; import java.math.BigInteger; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors;public class Main {static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));static int N = (int)1e4 + 10;static math math_bag = new math();static int fa[] = new int[N];public static void main(String[] args ) throws IOException, ParseException{String t = "2023年12月20日";SimpleDateFormat sd = new SimpleDateFormat("yyyy年MM月dd日"); // 需要和上邊的字符串的日期格式相同Date e = sd.parse(t);pw.println(e);pw.flush();} }class rd {static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));static StringTokenizer tokenizer = new StringTokenizer("");static String nextLine() throws IOException { return reader.readLine(); }static String next() throws IOException{while(!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());return tokenizer.nextToken();}static int nextInt() throws IOException { return Integer.parseInt(next()); }static double nextDouble() throws IOException { return Double.parseDouble(next()); }static long nextLong() throws IOException { return Long.parseLong(next()); }static BigInteger nextBigInteger() throws IOException{BigInteger d = new BigInteger(rd.nextLine());return d;} }class math {int gcd(int a,int b){if(b == 0) return a;else return gcd(b,a % b);}int lcm(int a,int b){return a * b / gcd(a, b);}// 求n的所有約數(shù)List get_factor(int n){List<Long> a = new ArrayList<>();for(long i = 1; i <= Math.sqrt(n) ; i ++){if(n % i == 0){a.add(i);if(i != n / i) a.add(n / i); // // 避免一下的情況:x = 16時,i = 4 ,x / i = 4的情況,這樣會加入兩種情況 ^-^復(fù)雜度能減少多少是多少}}// 相同因子去重,這個方法,完美a = a.stream().distinct().collect(Collectors.toList());// 對因子排序(升序)Collections.sort(a);return a;}// 判斷是否是質(zhì)數(shù)boolean check_isPrime(int n){if(n < 2) return false;for(int i = 2 ; i <= n / i; i ++) if (n % i == 0) return false;return true;} }class PII implements Comparable<PII> {int x,y;public PII(int x ,int y){this.x = x;this.y = y;}public int compareTo(PII a){if(this.x-a.x != 0)return this.x-a.x; //按x升序排序else return this.y-a.y; //如果x相同,按y升序排序} }class Edge {int a,b,c;public Edge(int a ,int b, int c){this.a = a;this.b = b;this.c = c;} }1、Date類
說明:Date日期類通常用于日期的計算
說明:Date中存儲的是距離一個歷史時間點(1970年)的毫秒數(shù)。
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"):創(chuàng)造一個日期格式,給Date來用
Date date = simpleDateFormat.parse("2000-1-1"):上面創(chuàng)建的日期格式后就可以給Date類型賦值了
long t = date.getTime():獲取時間,單位是毫秒
import java.io.*; import java.math.BigInteger; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors;public class Main {static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));static int N = (int)1e4 + 10;static math math_bag = new math();static int fa[] = new int[N];public static void main(String[] args ) throws IOException, ParseException{Date d = new Date();long t = d.getTime();pw.println(t);pw.flush();} }class rd {static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));static StringTokenizer tokenizer = new StringTokenizer("");static String nextLine() throws IOException { return reader.readLine(); }static String next() throws IOException{while(!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());return tokenizer.nextToken();}static int nextInt() throws IOException { return Integer.parseInt(next()); }static double nextDouble() throws IOException { return Double.parseDouble(next()); }static long nextLong() throws IOException { return Long.parseLong(next()); }static BigInteger nextBigInteger() throws IOException{BigInteger d = new BigInteger(rd.nextLine());return d;} }class math {int gcd(int a,int b){if(b == 0) return a;else return gcd(b,a % b);}int lcm(int a,int b){return a * b / gcd(a, b);}// 求n的所有約數(shù)List get_factor(int n){List<Long> a = new ArrayList<>();for(long i = 1; i <= Math.sqrt(n) ; i ++){if(n % i == 0){a.add(i);if(i != n / i) a.add(n / i); // // 避免一下的情況:x = 16時,i = 4 ,x / i = 4的情況,這樣會加入兩種情況 ^-^復(fù)雜度能減少多少是多少}}// 相同因子去重,這個方法,完美a = a.stream().distinct().collect(Collectors.toList());// 對因子排序(升序)Collections.sort(a);return a;}// 判斷是否是質(zhì)數(shù)boolean check_isPrime(int n){if(n < 2) return false;for(int i = 2 ; i <= n / i; i ++) if (n % i == 0) return false;return true;} }class PII implements Comparable<PII> {int x,y;public PII(int x ,int y){this.x = x;this.y = y;}public int compareTo(PII a){if(this.x-a.x != 0)return this.x-a.x; //按x升序排序else return this.y-a.y; //如果x相同,按y升序排序} }class Edge {int a,b,c;public Edge(int a ,int b, int c){this.a = a;this.b = b;this.c = c;} }計算兩天后的日期:
import java.io.*; import java.math.BigInteger; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors;public class Main {static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));static int N = (int)1e4 + 10;static math math_bag = new math();static int fa[] = new int[N];public static void main(String[] args ) throws IOException, ParseException{// 2天后的日期Date date = new Date();date.setTime(date.getTime() + 2L * 24 * 60 * 60 * 1000);SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");pw.println(s.format(date));pw.flush();} }class rd {static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));static StringTokenizer tokenizer = new StringTokenizer("");static String nextLine() throws IOException { return reader.readLine(); }static String next() throws IOException{while(!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());return tokenizer.nextToken();}static int nextInt() throws IOException { return Integer.parseInt(next()); }static double nextDouble() throws IOException { return Double.parseDouble(next()); }static long nextLong() throws IOException { return Long.parseLong(next()); }static BigInteger nextBigInteger() throws IOException{BigInteger d = new BigInteger(rd.nextLine());return d;} }class math {int gcd(int a,int b){if(b == 0) return a;else return gcd(b,a % b);}int lcm(int a,int b){return a * b / gcd(a, b);}// 求n的所有約數(shù)List get_factor(int n){List<Long> a = new ArrayList<>();for(long i = 1; i <= Math.sqrt(n) ; i ++){if(n % i == 0){a.add(i);if(i != n / i) a.add(n / i); // // 避免一下的情況:x = 16時,i = 4 ,x / i = 4的情況,這樣會加入兩種情況 ^-^復(fù)雜度能減少多少是多少}}// 相同因子去重,這個方法,完美a = a.stream().distinct().collect(Collectors.toList());// 對因子排序(升序)Collections.sort(a);return a;}// 判斷是否是質(zhì)數(shù)boolean check_isPrime(int n){if(n < 2) return false;for(int i = 2 ; i <= n / i; i ++) if (n % i == 0) return false;return true;} }class PII implements Comparable<PII> {int x,y;public PII(int x ,int y){this.x = x;this.y = y;}public int compareTo(PII a){if(this.x-a.x != 0)return this.x-a.x; //按x升序排序else return this.y-a.y; //如果x相同,按y升序排序} }class Edge {int a,b,c;public Edge(int a ,int b, int c){this.a = a;this.b = b;this.c = c;} }Date的使用例子:
標(biāo)題:第幾天
2000年的1月1日,是那一年的第1天。
那么,2000年的5月4日,是那一年的第幾天?
注意:需要提交的是一個整數(shù),不要填寫任何多余內(nèi)容。
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.math.BigInteger; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*;public class Main {static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));static int N = (int)500 + 10;static long a[][] = new long[N][N];static long s[][] = new long[N][N];public static void main(String[] args ) throws IOException, ParseException{SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");Date date1 = simpleDateFormat.parse("2000-1-1");Date date2 = simpleDateFormat.parse("2000-5-4");long t1 = date1.getTime();long t2 = date2.getTime();pw.println((t2 - t1) / (24*60*60*1000) + 1);pw.flush();} }class rd {static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));static StringTokenizer tokenizer = new StringTokenizer("");static String nextLine() throws IOException { return reader.readLine(); }static String next() throws IOException{while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());return tokenizer.nextToken();}static int nextInt() throws IOException { return Integer.parseInt(next()); }static double nextDouble() throws IOException { return Double.parseDouble(next()); }static long nextLong() throws IOException { return Long.parseLong(next());}static BigInteger nextBigInteger() throws IOException{BigInteger d = new BigInteger(rd.nextLine());return d;} }class PII {int x,y;public PII(int x ,int y){this.x = x;this.y = y;} }2、Calendar類
創(chuàng)建Calendar對象的API:
getInstance()
修改時間的API:
set():可以改變年、月、日、星期幾
add():add方法會產(chǎn)生其他相關(guān)時間屬性的連動變化
實戰(zhàn):
//set()方法:兩個參數(shù)【設(shè)置的項,設(shè)置的值】
calendar.set(Calendar.YEAR, year); // 存年
calendar.set(Calendar.MONTH, 11); // 存月,MONTH字段是從0月開始計數(shù)的,與數(shù)組類似,所以要想存12月,傳參數(shù)11,
calendar.set(Calendar.DAY_OF_MONTH, 31); //設(shè)置日期? 31號
calendar的使用例子:
1、世紀(jì)末的星期
標(biāo)題: 世紀(jì)末的星期
? ? 曾有邪教稱1999年12月31日是世界末日。當(dāng)然該謠言已經(jīng)不攻自破。
? ? 還有人稱今后的某個世紀(jì)末的12月31日,如果是星期一則會....
? ? 有趣的是,任何一個世紀(jì)末的年份的12月31日都不可能是星期一!!
? ? 于是,“謠言制造商”又修改為星期日......
? ? 1999年的12月31日是星期五,請問:未來哪一個離我們最近的一個世紀(jì)末年(即xx99年)的12月31日正好是星期天(即星期日)?
? ? 請回答該年份(只寫這個4位整數(shù),不要寫12月31等多余信息)
答案:2299
代碼:(此題使用java中的日歷類)
import javax.print.DocFlavor; import java.io.*; import java.math.BigInteger; import java.util.*;public class Main {static Scanner sc = new Scanner(System.in);static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));static int N = (int) 1e5 + 10;static PII a[] = new PII[N];static Set<Integer> set = new HashSet<>();public static void main(String[] args) throws IOException{Calendar calendar = Calendar.getInstance(); //獲取Calendar類的實例,這樣才能用for(int year=1999;year<10000;year+=100)//題目要求必須是xx99年 也就是每次加一百年{//set()方法:兩個參數(shù)【設(shè)置的項,設(shè)置的值】calendar.set(Calendar.YEAR, year); // 存年calendar.set(Calendar.MONTH, 11); // 存月,MONTH字段是從0月開始計數(shù)的,與數(shù)組類似,所以要想存12月,傳參數(shù)11,calendar.set(Calendar.DAY_OF_MONTH, 31); //設(shè)置日期 31號if (calendar.get(Calendar.DAY_OF_WEEK) == 1) //get()方法獲取值; DAY_OF_WEEK一周中的第幾天,星期日是第一天{ // 國外星期天對應(yīng)的是1 星期一對應(yīng)的是2 以此類推pw.println(year);break;}}pw.flush();} }class rd {static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));static StringTokenizer tokenizer = new StringTokenizer("");static String nextLine() throws IOException { return reader.readLine(); }static String next() throws IOException{while(!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());return tokenizer.nextToken();}static int nextInt() throws IOException { return Integer.parseInt(next()); }static double nextDouble() throws IOException { return Double.parseDouble(next()); }static long nextLong() throws IOException { return Long.parseLong(next()); }static BigInteger nextBigInteger() throws IOException{BigInteger d = new BigInteger(rd.nextLine());return d;} }class PII implements Comparable<PII> {int x,y;public PII(int x ,int y){this.x = x;this.y = y;}public int compareTo(PII a){if(this.x-a.x != 0)return this.x-a.x; //按x升序排序else return this.y-a.y; //如果x相同,按y升序排序} }class Edge {int a,b,c;public Edge(int a ,int b, int c){this.a = a;this.b = b;this.c = c;} }2、 跑步鍛煉
AC代碼1:()
import java.io.*; import java.math.BigInteger; import java.util.*; import java.util.stream.Collectors;public class Main {static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));static int N = (int)2e5 + 10;public static void main(String[] args ) throws IOException{int res = 0;Calendar c = Calendar.getInstance(); //建一個Calendarc.set(2000,0,1); // 把開始時間丟到Calendar里面去,注意月從0開始while (true){int day_of_month = c.get(Calendar.DAY_OF_MONTH); // 得到該天是該月的第幾天(得到該時間的日)int daya_of_week = c.get(Calendar.DAY_OF_WEEK); // 得到該天是該周的第幾天int year = c.get(Calendar.YEAR); //得到該時間的年int month = c.get(Calendar.MONTH); // 得到該時間的月if(day_of_month == 1 || daya_of_week == 2) res += 2;else res += 1;if(year == 2020 && month == 9 && day_of_month==1){pw.println(res);pw.flush();return;}c.add(Calendar.DATE,1); //時間增加一天}} }class rd {static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));static StringTokenizer tokenizer = new StringTokenizer("");static String nextLine() throws IOException { return reader.readLine(); }static String next() throws IOException{while(!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());return tokenizer.nextToken();}static int nextInt() throws IOException { return Integer.parseInt(next()); }static double nextDouble() throws IOException { return Double.parseDouble(next()); }static long nextLong() throws IOException { return Long.parseLong(next()); }static BigInteger nextBigInteger() throws IOException{BigInteger d = new BigInteger(rd.nextLine());return d;} }class math {int gcd(int a,int b){if(b == 0) return a;else return gcd(b,a % b);}int lcm(int a,int b){return a * b / gcd(a, b);}// 求n的所有約數(shù)List get_factor(int n){List<Long> a = new ArrayList<>();for(long i = 1; i <= Math.sqrt(n) ; i ++){if(n % i == 0){a.add(i);if(i != n / i) a.add(n / i); // // 避免一下的情況:x = 16時,i = 4 ,x / i = 4的情況,這樣會加入兩種情況 ^-^復(fù)雜度能減少多少是多少}}// 相同因子去重,這個方法,完美a = a.stream().distinct().collect(Collectors.toList());// 對因子排序(升序)Collections.sort(a);return a;}// 判斷是否是質(zhì)數(shù)boolean check_isPrime(int n){if(n < 2) return false;for(int i = 2 ; i <= n / i; i ++) if (n % i == 0) return false;return true;} }class PII implements Comparable<PII> {int x,y;public PII(int x ,int y){this.x = x;this.y = y;}public int compareTo(PII a){if(this.x-a.x != 0)return this.x-a.x; //按x升序排序else return this.y-a.y; //如果x相同,按y升序排序} }class Edge {int a,b,c;public Edge(int a ,int b, int c){this.a = a;this.b = b;this.c = c;} }class Line implements Comparable<Line> {double k; // 斜率double b; // 截距public Line(double k, double b){this.k = k;this.b = b;}@Overridepublic int compareTo(Line o){if (this.k > o.k) return 1;if (this.k == o.k){if (this.b > o.b) return 1;return -1;}return -1;} }class mqm {int fa[] = new int[1005];void init(){for(int i = 1 ; i <= 1000 ; i ++) fa[i] = i;}void merge(int x, int y) { fa[find(x)] = find(y); }int find(int x){if(x != fa[x]) fa[x] = find(fa[x]);return fa[x];}boolean query(int x, int y) { return find(x) == find(y); } }AC代碼2:(LocalDate真的優(yōu)美!!!)
import java.time.LocalDate;public class Main {public static void main(String[] args) {LocalDate startTime = LocalDate.of(2000, 1, 1); // 設(shè)置開始時間LocalDate endtTime = LocalDate.of(2020, 10, 1); // 設(shè)置結(jié)束時間int allKM = 0;while(!startTime.isAfter(endtTime)) // 開始時間不等于最終時間{if(startTime.getDayOfWeek().getValue() == 1 || startTime.getDayOfMonth() == 1) allKM += 2; // 周一或者每個月的一號,跑兩千米else allKM += 1; startTime = startTime.plusDays(1); // 時間+1}System.out.println(allKM); // 輸出總公里數(shù)} }3、
代碼:
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.math.BigInteger; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*;public class Main {static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));static int N = (int)3000 + 10;public static void main(String[] args ) throws IOException, ParseException{Calendar calendar = Calendar.getInstance();calendar.set(2022,0,1);int cnt = 0;while(true){int year = calendar.get(Calendar.YEAR);int month = calendar.get(Calendar.MONTH);int day_of_month = calendar.get(Calendar.DAY_OF_MONTH);int day_of_week = calendar.get(Calendar.DAY_OF_WEEK);if(day_of_week == 7 || day_of_week == 1 || day_of_month == 1 || day_of_month == 11 || day_of_month == 21 || day_of_month == 31) cnt ++;if(year == 2022 && month == 11 && day_of_month == 31){pw.println(cnt);pw.flush();return;}calendar.add(Calendar.DATE,1);}// Calendar calendar = Calendar.getInstance(); // // calendar.set(2000,0,1); // int year = calendar.get(Calendar.YEAR); // int month = calendar.get(Calendar.MONTH); // int day_of_month = calendar.get(Calendar.DAY_OF_MONTH); // int day_of_week = calendar.get(Calendar.DAY_OF_WEEK); // // pw.println(year); // pw.println(month); // pw.println(day_of_month); // pw.println(day_of_week); // pw.flush();} }class rd {static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));static StringTokenizer tokenizer = new StringTokenizer("");static String nextLine() throws IOException { return reader.readLine(); }static String next() throws IOException{while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());return tokenizer.nextToken();}static int nextInt() throws IOException { return Integer.parseInt(next()); }static double nextDouble() throws IOException { return Double.parseDouble(next()); }static long nextLong() throws IOException { return Long.parseLong(next());}static BigInteger nextBigInteger() throws IOException { return new BigInteger(rd.nextLine()); } }class PII {int x,y;public PII(int x ,int y){this.x = x;this.y = y;} }總結(jié)
以上是生活随笔為你收集整理的JAVA-日期类(Date、SimpleDateFormat)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 话说中国足球
- 下一篇: 机器学习多步时间序列预测解决方案