一.java.util.Date类
两个构造器的使用:
Date():创建一个对应当前时间的Date对象
Date(long date):创建一个指定毫秒数的Date对象
两个方法的使用:
toString():显式当前的年月日时分秒
getTime():获取当前对象时毫秒数(时间戳)
public void test2(){
//构造器一:Date():创建一个对应当前时间的Date对象
Date date1 = new Date();
System.out.println(date1.toString());
System.out.println(date1.getTime());
//构造器二:Date(long date):创建一个指定毫秒数的Date对象
Date date2 = new Date(1595473439050L);
System.out.println(date2.toString());
System.out.println(date2.getTime());
}
java.sql.Date类
java.sql.Date类是java.util.Date类的子类
java.sql.Date类对应着数据库中的日期类型的变量
实例化:
public void test2(){
java.sql.Date date3 = new java.sql.Date(1595473439050L);
System.out.println(date3.toString());
}
二.SimpleDateFormat的使用
SimpleDateFormat对日期Date类的两个操作:
格式化: 日期→字符串
解析: 格式化的逆过程,字符串→日期
SimpleDateFormat的实例化:
public void test3() throws ParseException {
//实例化SimpleDateFormat,使用默认的构造器
SimpleDateFormat sdf = new SimpleDateFormat();
//格式化:日期→字符串
Date date = new Date();
System.out.println(date);
String format = sdf.format(date);
System.out.println(format);
//解析:格式化的逆过程,字符串→日期
String str = "20-7-23 下午12:25";//注意时间格式必须严格一致
Date date1 = sdf.parse(str);
System.out.println(date1);
}
上述的解析需要时间格式严格的一致,但是我们在开发中不喜欢这么严格的格式,怎么办呢?——实例化SimpleDateFormat,使用带参数的构造器
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
三.java.util.Calendar日历类
- calendar是一个抽象基类,主要用于完成日期字段之间相互操作的功能
- calendar实例化的方法:①使用其静态方法:Calendar.getInstance()方法 ②创建他的子类GregorianCalendar的对象
- 常用方法:get(), set(),add(),getTime(),setTime()
public void test4(){
//1.实例化
//方式一:创建他的子类GregorianCalendar的对象
//方式二:使用其静态方法:Calendar.getInstance()方法
Calendar calendar = Calendar.getInstance();
//2.常用方法
//get()
int days = calendar.get(Calendar.DAY_OF_MONTH);//当前日子是这个月的第几天,我这里是23天
System.out.println(days);
//set()
calendar.set(Calendar.DAY_OF_MONTH,25);//把当前日子是这个月的第23天改为第25天
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
//add()
calendar.add(Calendar.DAY_OF_MONTH,3);//在原有的日子基础上 再加上3天
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
//getTime():把calendar类→date
Date date = calendar.getTime();
System.out.println(date);
//setTime():把date→calendar类
Date date1 = new Date();
calendar.setTime(date1);
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
}
注意:
获取月份时:一月是0,二月是1,以此类推,12月是11
获取星期时:周日是1,周二是2,以此类推,周六是7
四.LocalDate、LocalTime、LocalDateTime的使用
public void test5(){
//实例化的第一种方式:now():获取当前的日期、时间、日期+时间
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);
//实例化的第二种方式:of():设置指定的年月日时分秒(没有偏移量的)
LocalDateTime localDateTime1 = LocalDateTime.of(2010, 11, 27, 15, 24);
System.out.println(localDateTime1);
//getXxx():获取相关的属性
System.out.println(localDate.getDayOfMonth());
System.out.println(localDate.getDayOfWeek());
System.out.println(localDate.getDayOfYear());
//withXxx():设置相关的属性
LocalDateTime localDateTime2 = localDateTime.withDayOfMonth(12);
System.out.println(localDateTime);
System.out.println(localDateTime2);
}
五.Instant类的使用
public void test6(){
//now():获取本初子午线处对应的标准时间
Instant instant = Instant.now();
System.out.println(instant);
//根据时区添加时间的偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);
//toEpochMilli():获取瞬时点自1970年1月1号0时0分0秒对应的毫秒数
long milli = instant.toEpochMilli();
System.out.println(milli);
//ofEpochMilli():通过给定的毫秒数,获取Instant实例
Instant instant1 = Instant.ofEpochMilli(1595490880839l);
System.out.println(instant1);
}
六.DateTimeFormatter的使用
DateTimeFormatter:格式化或解析日期、时间,类似于SimpleDateFormat
方式一:预定义的标准格式。如:ISO_LOCAL_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
方式二:
本地化相关的格式,如ofLocalizedDateTime()
FormatStyle.LONG/FormatStyle.MEDIUM/FormatStyle.SHORT:适用于LocalDateTime
本地化相关的格式,如ofLocalizedDate()
FormatStyle.FULL/FormatStyle.LONG/FormatStyle.MEDIUM/FormatStyle.SHORT:适用于LocalDate
重点:方式三:自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
public void test7(){
//方式一:预定义的标准格式。如:ISO_LOCAL_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
//格式化:日期→字符串
LocalDateTime localDateTime = LocalDateTime.now();
String str1 = formatter.format(localDateTime);
System.out.println(localDateTime);
System.out.println(str1);//2020-07-23T16:17:01.236
//解析:字符串→日期
TemporalAccessor parse = formatter.parse("2020-07-23T16:17:01.236");
System.out.println(parse);
//方式二:本地化相关的格式,如ofLocalizedDateTime()
//FormatStyle.LONG/FormatStyle.MEDIUM/FormatStyle.SHORT:适用于LocalDateTime
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
//格式化
String str2 = formatter1.format(localDateTime);
System.out.println(str2);
//本地化相关的格式,如ofLocalizedDate()
//FormatStyle.FULL/FormatStyle.LONG/FormatStyle.MEDIUM/FormatStyle.SHORT:适用于LocalDate
DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
//格式化
String str3 = formatter2.format(LocalDate.now());
System.out.println(str3);
//重点:方式三:自定义的格式。如:ofPattern("yyyy-MM-dd hh:mm:ss")
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
//格式化
String str4 = formatter3.format(LocalDateTime.now());
System.out.println(str4);
//解析
TemporalAccessor accessor = formatter3.parse("2020-07-23 04:27:52");
System.out.println(accessor);
}
七.其他API
八.System类
static void exit(int status);//该方法用于终止当前正在运行的Java虚拟机,其中参数status表示状态码,若状态码非0,则表示异常终止
static void gc();//运行垃圾回收器,并对垃圾进行回收
static native long currentTimeMills();//返回以毫秒为单位的当前时间
static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length);//从src引用的指定源数组拷贝到dest引用的数组,拷贝从指定的位置开始,到目标数组的指定位置结束
static Properties getProperties();//取得当前的系统的属性
static String getProperty(String key);//获得指定键描述的系统属性
getProperties()方法
import java.util.*;
public class Example09{
public static void main(String[] args){
//获取当前系统属性
Properties properties=System.getProperties();
System.out.println(properties);
//获取所有系统属性的key(属性名),返回set对象
Set<String>propertyNames=properties.stringPropertyNames();
for(String key : propertyNames){
//获取当前键key(属性名)所对应的值(属性值)
String value=System.getPropertty(key);
System.out.println(key+"--->"+value);
}
}
}
九.java格式化数字 NumberFormat及DecimalFormat
首先,要特别注意的是 NumberFormat和DecimalFormat是线程不安全的。 这意味你如果同时有多个线程操作一个format实例对象,会出现意想不到的结果。
JavaAPI官方描述
NumberFormat
NumberFormat帮助您格式化和解析任何区域设置的数字。您的代码可以完全独立于小数点,千位分隔符的区域设置约定,甚至是使用的特定十进制数字,或者数字格式是否为十进制。
DecimalFormat
DecimalFormat是NumberFormat十进制数字格式的具体子类 。它具有多种功能,旨在解析和格式化任何语言环境中的数字,包括支持西方,阿拉伯语和印度语数字。它还支持不同类型的数字,包括整数(123),定点数(123.4),科学记数法(1.23E4),百分比(12%)和货币金额(123美元)。所有这些都可以本地化。
NumberFormat
获取NumberFormat实例
//创建 一个整数格式 地区用系统默认的
NumberFormat integerNumber = NumberFormat.getIntegerInstance(Locale.getDefault());
- 使用getInstance或getNumberInstance获取正常的数字格式。
- 使用getIntegerInstance得到的整数格式。
- 使用getCurrencyInstance来获取货币数字格式。
- 使用getPercentInstance获取显示百分比的格式。
常用方法
使用示例
DecimalFormat是NumberFormat,所以,就不要单独的为NumberFormat写一个完整的示例了。只写一下配合FieldPosition怎么使用的示例:
NumberFormat numberFormat = NumberFormat.getInstance(Locale.getDefault());
//整数部分不会每隔三个,就会有 " ,"
numberFormat.setGroupingUsed(false);
//线程安全的字符串缓冲类
StringBuffer stringBuffer = new StringBuffer();
//构造参数 是Format子类里面的 自己特有的参数,传入就行
//构造 小数部分的,所以开始 beginIndex()是从小数点 后面算的, 但是0是从整个格式化数字,第一个算起, 包括 之间用于分组的 " ,"
FieldPosition fieldPosition = new FieldPosition(NumberFormat.FRACTION_FIELD);
stringBuffer = numberFormat.format(1234.56789, stringBuffer, fieldPosition);
System.out.println(stringBuffer.toString());
//小数部分, 所以 从5 开始
System.out.println(fieldPosition.getBeginIndex() + " " + fieldPosition.getEndIndex());
//切割字符串
System.out.println(stringBuffer.toString().substring(fieldPosition.getBeginIndex()));
DecimalFormat
获取DecimalFormat实例
要获取特定地区(包括默认地区)的NumberFormat,请调用NumberFormat的工厂方法之一,例如getInstance()。通常,不要直接调用DecimalFormat构造函数,因为NumberFormat工厂方法可能返回DecimalFormat之外的子类。如果需要自定义format对象,可以这样做:
try {
NumberFormat f = NumberFormat.getInstance(Locale.getDefault());
if (f instanceof DecimalFormat) {
((DecimalFormat) f).setDecimalSeparatorAlwaysShown(true);
//写具体的代码
}
} catch (Exception e) {
e.printStackTrace();
}