package cn.sxt.test;
import java.util.Date;
import java.util.Date;
public class TestData {
public static void main(String[] args) {
Date d=new Date(2000);//标准时间之后的毫秒数
System.out.println(d);
System.out.println(d.getTime());
Date d2=new Date();//当前时刻
System.out.println(d2.getTime());
System.out.println(d2.after(d));
}
}
DateFormat类的作用把时间对象转化成字符串或者把字符串对象转换为时间
DateFormat是一个抽象类,一般使用他的子类SimpleDateFormat类来实现
package Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.DateFormat;
public class DataFormat {
public static void main(String[] args) throws ParseException {
//把时间转换为字符串
DateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String str1=df.format(400000);
System.out.println(str1);
//把字符串转换为时间
DateFormat df2=new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒");
Date date=df2.parse("1983年5月10日 10时45分59秒");
System.out.println(date);
//测试其他字符
DateFormat df3=new SimpleDateFormat("D");//今年的第多少天
String str3=df3.format(new Date());//new Date()表示现在的时间
System.out.println(str3);
}
}
?
?
package Data;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class Calendar日历类 {
public static void main(String[] args) {
//获得日期的相关元素
Calendar calendar = new GregorianCalendar(2999, 10, 9, 22, 10, 50);
int year = calendar.get(Calendar.YEAR);
int day = calendar.get(Calendar.DATE);//也可以用DAY_OF_MONTH
int month = calendar.get(Calendar.MONTH);
int weekday = calendar.get(Calendar.DAY_OF_WEEK);//星期几 1是星期日
System.out.println(year);//0是一月
System.out.println(month);
System.out.println(weekday);
System.out.println(day);
//设置日期的元素
Calendar c2 = new GregorianCalendar();//什么都不填默认今天的日期
c2.set(Calendar.YEAR, 8012);
System.out.println(c2);
//日期的计算
Calendar c3 = new GregorianCalendar();
c3.add(Calendar.DATE, 100);//往后100天
c3.add(Calendar.YEAR, -100);//往前100年
System.out.println(c3);
//日期对象和时间对象的转换
Date d4 = c3.getTime();//转换为时间
Calendar c4 = new GregorianCalendar();//抽象类不能实体化对象
c4.setTime(new Date());//时间转换成日期
printCalendar(c4);
}
public static void printCalendar(Calendar c) {
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int date = c.get(Calendar.DAY_OF_MONTH);
int dayweek = c.get(Calendar.DAY_OF_WEEK)-1;
String dayweek2=dayweek==0?"日":dayweek+"";
int hour=c.get(Calendar.HOUR);
int minute=c.get(Calendar.MINUTE);
int second=c.get(Calendar.SECOND);
System.out.println(year+"年"+month+"月"+date+"日"+hour+"时"+minute+"分"+second+"秒"+" 周"+dayweek2);
}
}
package Data;
import java.text.DateFormat;
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 可视化日历 {
public static void main(String[] args) throws ParseException {
System.out.println("请输入日期(格式2020-9-3)");
Scanner scanner=new Scanner(System.in);
String str=scanner.nextLine();
DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
Date date=df.parse(str);
Calendar c=new GregorianCalendar();
c.setTime(date);
int day=c.get(Calendar.DAY_OF_MONTH);
int days=c.getActualMaximum(Calendar.DATE);
System.out.println("日\t一\t二\t三\t四\t五\t六");
c.set(Calendar.DAY_OF_MONTH,1);//从每月的一号开始
for(int i=0;i<c.get(Calendar.DAY_OF_WEEK)-1;i++){
System.out.print("\t");
}
for(int i=1;i<=days;i++){
if(day==c.get(Calendar.DAY_OF_MONTH)){
System.out.print(c.get(Calendar.DAY_OF_MONTH)+"*\t");
} else {
System.out.print(c.get(Calendar.DAY_OF_MONTH) + "\t");
}
if(c.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY){
System.out.println();//换行
}
c.add(Calendar.DAY_OF_MONTH,1);
}
}
}
|