目录
java.util.Date日期类
java.text.SimpleDateFormat日期转换类
SimpleDateFormat:实现String和Date之间的转换
1、 日期转字符串
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();
// 创建日期格式对象
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
// 日期转换为字符串
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的所有约数
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的情况,这样会加入两种情况 ^-^复杂度能减少多少是多少
}
}
// 相同因子去重,这个方法,完美
a = a.stream().distinct().collect(Collectors.toList());
// 对因子排序(升序)
Collections.sort(a);
return a;
}
// 判断是否是质数
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、字符串转换为日期(用的少)
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的所有约数
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的情况,这样会加入两种情况 ^-^复杂度能减少多少是多少
}
}
// 相同因子去重,这个方法,完美
a = a.stream().distinct().collect(Collectors.toList());
// 对因子排序(升序)
Collections.sort(a);
return a;
}
// 判断是否是质数
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年)的毫秒数。
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"):创造一个日期格式,给Date来用
Date date = simpleDateFormat.parse("2000-1-1"):上面创建的日期格式后就可以给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的所有约数
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的情况,这样会加入两种情况 ^-^复杂度能减少多少是多少
}
}
// 相同因子去重,这个方法,完美
a = a.stream().distinct().collect(Collectors.toList());
// 对因子排序(升序)
Collections.sort(a);
return a;
}
// 判断是否是质数
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的所有约数
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的情况,这样会加入两种情况 ^-^复杂度能减少多少是多少
}
}
// 相同因子去重,这个方法,完美
a = a.stream().distinct().collect(Collectors.toList());
// 对因子排序(升序)
Collections.sort(a);
return a;
}
// 判断是否是质数
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的使用例子:
标题:第几天
2000年的1月1日,是那一年的第1天。
那么,2000年的5月4日,是那一年的第几天?注意:需要提交的是一个整数,不要填写任何多余内容。
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类
创建Calendar对象的API:
getInstance()
修改时间的API:
set():可以改变年、月、日、星期几
add():add方法会产生其他相关时间属性的连动变化
实战:
//set()方法:两个参数【设置的项,设置的值】
calendar.set(Calendar.YEAR, year); // 存年
calendar.set(Calendar.MONTH, 11); // 存月,MONTH字段是从0月开始计数的,与数组类似,所以要想存12月,传参数11,
calendar.set(Calendar.DAY_OF_MONTH, 31); //设置日期 31号
calendar的使用例子:
1、世纪末的星期
标题: 世纪末的星期
曾有邪教称1999年12月31日是世界末日。当然该谣言已经不攻自破。
还有人称今后的某个世纪末的12月31日,如果是星期一则会....
有趣的是,任何一个世纪末的年份的12月31日都不可能是星期一!!
于是,“谣言制造商”又修改为星期日......
1999年的12月31日是星期五,请问:未来哪一个离我们最近的一个世纪末年(即xx99年)的12月31日正好是星期天(即星期日)?
请回答该年份(只写这个4位整数,不要写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()方法:两个参数【设置的项,设置的值】
calendar.set(Calendar.YEAR, year); // 存年
calendar.set(Calendar.MONTH, 11); // 存月,MONTH字段是从0月开始计数的,与数组类似,所以要想存12月,传参数11,
calendar.set(Calendar.DAY_OF_MONTH, 31); //设置日期 31号
if (calendar.get(Calendar.DAY_OF_WEEK) == 1) //get()方法获取值; DAY_OF_WEEK一周中的第几天,星期日是第一天
{ // 国外星期天对应的是1 星期一对应的是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(); //建一个Calendar
c.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的所有约数
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的情况,这样会加入两种情况 ^-^复杂度能减少多少是多少
}
}
// 相同因子去重,这个方法,完美
a = a.stream().distinct().collect(Collectors.toList());
// 对因子排序(升序)
Collections.sort(a);
return a;
}
// 判断是否是质数
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;
}
@Override
public 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真的优美!!!)
import java.time.LocalDate;
public class Main
{
public static void main(String[] args)
{
LocalDate startTime = LocalDate.of(2000, 1, 1); // 设置开始时间
LocalDate endtTime = LocalDate.of(2020, 10, 1); // 设置结束时间
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); // 输出总公里数
}
}
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;
}
}