欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

13_日期时间、Math、枚举

程序员文章站 2022-06-28 21:18:10
日期时间、Math、枚举 日期时间 计算机如何表示时间? GMT时间指格林尼治所在地的标准时间,也称为时间协调时(UTC),其他地区的时间都是相对于GMT时间的偏移。 北京位于东八区 = UTC + 8 时间戳(timestamp):距离特定时间经过的毫秒数,这个特定的时间计算机规定了是1970年0 ......
日期时间、math、枚举
日期时间
计算机如何表示时间?
gmt时间指格林尼治所在地的标准时间,也称为时间协调时(utc),其他地区的时间都是相对于gmt时间的偏移。
 
北京位于东八区 = utc + 8
 
时间戳(timestamp):距离特定时间经过的毫秒数,这个特定的时间计算机规定了是1970年01月01日 00:00:00:000,这个特定时间成为历元。
 
计算机时间2018.12.22 11:41:23:000 就是2018.12.22 11:41:23:000距离历元经历的毫秒数,用long类型存储。
 
date
java中表示日期时间的类,位于java.util包中。
 
public static void main(string[] args) {
// 根据当前系统当前时区构造一个date对象表示当前时间。
date date = new date();
// tue apr 02 14:25:06 cst(china standard time) 2019
system.out.println(date);
 
 
date date2 = new date(1000);
// thu jan 01 08:00:00 cst 1970
system.out.println(date2);
 
 
// 需求:创建一个时间表示1天之后的现在
long ts1 = 24 * 60 * 60 * 1000;
date now = new date();
long ts2 = now.gettime() + ts1;
date date3 = new date(ts2);
system.out.println(date3);
}
date 本质上是一个包装类,内部封装了一个long 类型的timestamp,表示距离历元所经历 的毫秒数。可以通过gettime() 其内部存储的时间戳。
 
时间的比较
public static void main(string[] args) {
 
date date1 = new date();
 
long ts1 = 24 * 60 * 60 * 1000;
date date2 = new date(date1.gettime() + ts1);
 
// before/after
system.out.println(date1.before(date2));
system.out.println(date1.after(date2));
 
// comparable接口
system.out.println(date1.compareto(date2));
 
// equals
system.out.println(date1.equals(date2));
}
 
日期时间格式化
[1] date -> 字符串
public static void main(string[] args) {
 
date now = new date();
// system.out.println(now.tostring());
 
// 格式化成友好的时间
// 按照默认的模式和默认的语言环境创建格式化对象
simpledateformat df = new simpledateformat();
// 19-4-2 下午2:51
string nowstr = df.format(now);
system.out.println(nowstr);
 
// 需求:按照 年-月-日 时:分:秒 格式格式化时间
/*
simpledateformat df2 = new simpledateformat("yyyy年mm月dd日 hh:mm:ss");
string nowstr2 = df2.format(now);
system.out.println(nowstr2);
*/
 
// applypattern
simpledateformat df3 = new simpledateformat();
df3.applypattern("yyyy年mm月dd日");
string nowstr3 = df3.format(now);
system.out.println(nowstr3);
 
df3.applypattern("hh:mm:ss");
string nowstr4 = df3.format(now);
system.out.println(nowstr4);
}
 
[2] 字符串 -> date
public class test04 {
public static void main(string[] args) {
 
string str1 = "2019年04月02日 15:34:12";
 
simpledateformat df = new simpledateformat();
df.applypattern("yyyy年mm月dd日 hh:mm:ss");
 
date date = null;
try {
date = df.parse(str1);
} catch (parseexception e) {
e.printstacktrace();
}
 
system.out.println(date);
}
}
 
注意:parse方法会产生parseexcepion 检查时异常。
 
 
日历calendar
日历本质上封装了一个long类型的time,表示距离历元的毫秒数。
calendar内部提供了很多方法用于计算这个时间戳表示的一些信息。这些信息用字段(year、month)标记,可以通过get()方法获取,计算好的字段信息放到数组中,通过字段(索引)到数组中取值而已。
 
通常通过getinstance()工厂方法获取日历对象。
 
获取日历中的基本信息
public static void main(string[] args) {
// 更加当前语言环境、当前时区创建一个日历对象
calendar cal = calendar.getinstance();
system.out.println(cal);
 
// 获取日历中的年(year)
int year = cal.get(calendar.year);
// 获取月份(0-11),月份从0开始
int month = cal.get(calendar.month);
// 获取一个月中的第几天,从1开始
int day = cal.get(calendar.day_of_month);
 
// 获取一天的第几个小时(0-23)
int hour = cal.get(calendar.hour_of_day);
int min = cal.get(calendar.minute);
int second = cal.get(calendar.second);
int millsecond = cal.get(calendar.millisecond);
 
// 获取星期,从周日开始
int week = cal.get(calendar.day_of_week);
system.out.println(week);
 
// 获取一个月有多少天?
int maxday = cal.getactualmaximum(calendar.day_of_month);
int minday = cal.getactualminimum(calendar.day_of_month);
 
}
 
需求:控制台输入本月日历
package cn.sxt03.calender;
import java.util.calendar;
public class test03 {
public static void main(string[] args) {
 
calendar cal = calendar.getinstance();
cal.set(calendar.month, 5);
// 记录今天的日期
int today = cal.get(calendar.date);
 
// 修改日历到本月1号
cal.set(calendar.date, 1);
// 本月1号是星期几
int weekoffirstday = cal.get(calendar.day_of_week);
system.out.println(weekoffirstday);
 
int onedayinmonth = cal.getactualminimum(calendar.day_of_month);
int lastdayinmonth = cal.getactualmaximum(calendar.day_of_month);
 
// 修改日历到本月今天
cal.set(calendar.date, today);
 
system.out.println("日\t一\t二\t三\t四\t五\t六");
// 输出本月1号前的空格
for(int i=1;i<weekoffirstday;i++) {
system.out.print("\t");
}
// 排列1-最大天数
for(int i=onedayinmonth;i<=lastdayinmonth;i++) {
// 修改日历到指定的天
cal.set(calendar.date, i);
 
if(i == today) {
system.out.print(i+"*\t");
}else {
system.out.print(i+"\t");
}
 
if(cal.get(calendar.day_of_week) == calendar.saturday) {
system.out.println();
}
 
}
 
}
}
 
math类
math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
public static void main(string[] args) {
// 属性
system.out.println(math.pi);
 
// 【1】求绝对值
system.out.println(math.abs(-1));
 
// 【2】ceil/floor
// ceil(x) 返回比x大的最小整数 => 向上取整
double a = 10.1;
system.out.println(math.ceil(a));
// floor(x) 返回比x小的最大整数 => 向下取整
system.out.println(math.floor(a));
 
// 【3】求两个数的最大值、最小值
system.out.println(math.max(10, 20));
system.out.println(math.min(10, 20));
 
// 【4】pow(x,y) 求x的y次幂
system.out.println(math.pow(2, 3));
 
// 【5】random()
 
// 【6】round 四舍五入
system.out.println(math.round(10.5));
 
// sqrt() 开方
system.out.println(math.sqrt(4));
 
// 【7】角度->弧度
system.out.println(math.toradians(180));
// 弧度->角度
system.out.println(math.todegrees(math.pi/3));
 
// 【8】sin/cos/tan(c) 参数是弧度
system.out.println(math.sin(math.pi/6));
 
// 需求:求两点之间的距离(x1,y1)-(x2,y2)
// math.sqrt(math.pow((x1-x2), 2)+math.pow((y1-y2), 2))
}
 
枚举
枚举概念
枚举(enum)是一组固定常量的组成的类型,它是一种自定义数据类型。
[修饰符] enum 枚举名称{
常量1,
常量2,
 
常量n[;]
}
注意:
[1]枚举中的常量是有限的。
[2] 枚举值不需要任何修饰符,默认static final 类型
 
需求:声明一个性别的枚举
public enum gender{
男,
女,
保密
}
声明了一个枚举,它是一种数据类型,可以声明变量。
 
public class test01 {
public static void main(string[] args) {
 
// 声明了一个gender类型的变量
gender gender = gender.;
system.out.println(gender);
}
}
 
总结:
[1]枚举类型是除基本数据类型、引用数类型外的另外一种自定义数据类型。一般可以把有限的、可列举的常量值定义成枚举类型。
[2] 通过gender枚举声明的变量取值一定是gender中声明的枚举值,不能取其他值。
 
 
enum 本质(c)
enum 本质上是继承于enum类。enum实际上也是类。
public enum gender{
// 枚举值本质上是gender类型的一个实例
,
,
保密;
 
private gender() {
system.out.println("gender");
}
 
public void showinfo() {
system.out.println("showinfo");
}
}
 
枚举和string的转换
public class test02 {
public static void main(string[] args) {
 
// 【1】枚举值->string
gender gender = gender.;
system.out.println(gender.tostring());
 
// 【2】string->枚举值
string str = "女";
// 可能会illegalargumentexception
gender gender2 = enum.valueof(gender.class, str);
system.out.println(gender2);
}
}
 
其他方法
public class test03 {
public static void main(string[] args) {
 
// name() 获取枚举值的名称
// equals() 比较两个枚举值是否相等
 
gender gender = gender.保;
system.out.println(gender.ordinal());
 
}
}
 
枚举和switch
public static void main(string[] args) {
 
action action = action.del;
 
switch (action) {
case add:{
system.out.println("add");
break;
}
case view:{
system.out.println("view");
break;
}
case del:{
system.out.println("del");
break;
}
case exit:{
system.out.println("exit");
break;
}
}
}
 
 
file 类
java中文件或者目录都可以通过file类来表示。
 
public static void main(string[] args) {
// 路径分隔符。
/**
* win=>;
* mac|linux|unix=>:
*/
system.out.println(file.pathseparator);
// 路径名称分隔符
/**
* win=> \
* mac|linux|unix => /
*/
system.out.println(file.separator);
}