计算某年、某月、某日和某年、某月、某日之间的天数间隔和周数。
package com.oracle.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Lianxi06 {
public static void main(String[] args) throws ParseException {
Scanner sc = new Scanner(System.in);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("请输入第一个日期:(yyyy-mm-dd)");
String date1 = sc.next();
System.out.println("请输入第二个日期:(yyyy-mm-dd)");
String date2 = sc.next();
//把字符串类型解析为Date类型
Date d1 = sdf.parse(date1);
Date d2 = sdf.parse(date2);
long i = 0;
if(d1.after(d2)){
i = d1.getTime() - d2.getTime();
}else{
i = d2.getTime() - d1.getTime();
}
// 分钟 = 1000*60 ; 小时 = 分钟*60 ; 天=小时*24
long a = 86400000;
long day = i/a;
long week = day/7;
System.out.println("两个日期相隔"+day+"天");
System.out.println("相隔"+week+"周");
}
}
============================================================
计算并输出21世纪的闰年,计算程序的执行时间。
package com.oracle.test;
import java.util.GregorianCalendar;
public class Lianxi07 {
public static void main(String[] args) {
long start = System.currentTimeMillis();
GregorianCalendar g = new GregorianCalendar();
for (int i = 2000; i < 2100; i++) {
if(g.isLeapYear(i)){
System.out.println(i+"是闰年");
}
}
long end = System.currentTimeMillis();
System.out.println("程序运行时间为"+(end-start)+"毫秒");
}
}
=======================================================
.编写一个程序,设定一个有大小写字母的字符串,先将字符串的大写字符输出,再将字符串中的小写字符输出。
package com.oracle.test;
public class Lianxi08 {
public static void main(String[] args) {
String str = new Lianxi02().getStr(10);
System.out.println(str);
char[] cs = str.toCharArray();
System.out.print("大写字母:");
for (char c : cs) {
if(Character.isUpperCase(c)){
System.out.print(c+" ");
}
}
System.out.print("\n小写字母:");
for (char c : cs) {
if(Character.isLowerCase(c)){
System.out.print(c+" ");
}
}
}
}
===========================================================
编写程序,(Scanner)当以年-月-日的格式输入一个日期时,输出其该年是否为闰年,该月有几天,该日是星期几
package com.oracle.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;
public class Lianxi09 {
public static void main(String[] args) throws ParseException {
Scanner sc = new Scanner(System.in);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("请输入第一个日期:(yyyy-mm-dd)");
String date = sc.next();
Date d = sdf.parse(date);
//把Date类型转为Calendar类型
Calendar c = Calendar.getInstance();
c.setTime(d);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;
int week = c.get(Calendar.DAY_OF_WEEK)-1;
GregorianCalendar g = new GregorianCalendar();
if(g.isLeapYear(year)){
System.out.println(year+"是闰年");
}else{
System.out.println(year+"是平年");
}
int max = c.getActualMaximum(Calendar.DAY_OF_MONTH);
String s = week==0? "周日" : "周"+week;
System.out.println(month+"月有"+max+"天,该日是"+s);
}
}
来源:CSDN
作者:凯凯王
链接:https://blog.csdn.net/fzhwk/article/details/78337944