日期和fastjson工具类

Date

构造方法

// 使用给定的毫秒时间值构造一个 Date对象。
Date date =  new Date(long date) 
Date date = new Date();

Mon Jan 05 04:35:33 CST 1970

实例方法

date.setTime(333333333);
date.getTime();
date1.before(date2); ——boolean
date1.after(date2);  ——boolean
date.compareTo(oldDate)  ————int
小返回-1 大返回1 相同则返回0

LocalDate

静态方法

LocalDate localDate= LocalDate.now();

LocalDate localDate= LocalDate.of(2020, 10, 15);

LocalDate localDate= LocalDate.ofYearDay(2024, 180);

LocalDate localDate= LocalDate.parse("2020-12-12");

实例方法

-- day of ?
int dayOfWeek = localDate.getDayOfWeek();
int dayOfMonth = localDate.getDayOfMonth();
int dayOfYear = localDate.getDayOfYear();
-- month of ?
int monthOfYear = localDate.getMonthOfYear();

int centuryOfEra = localDate.getCenturyOfEra(); // 20 
int yearOfCentury = localDate.getYearOfCentury();// 24
lengthOfMonth() 
lengthOfYear() 



// 字符串和localdate的相互转换
String format = localDate.format(dateTimeFormatter);

// 比较两个localdate
// 负数为小 正数 大  相同 0
int i = localDate.compareTo(localdate2); 

// localdate的比较 返回值均为Boolean类型
isAfter(ChronoLocalDate other) 
isBefore(ChronoLocalDate other) 
isEqual(ChronoLocalDate other) 
// 是否是闰年
isLeapYear() 

// 减去一段时间
LocalDate localDate1 = localDate.minusYears(10);
LocalDate localDate2 = localDate.minusWeeks(8);
LocalDate localDate3 = localDate.minusMonths(1);
LocalDate localDate4 = localDate.minusDays(100);

//增加一段时间
LocalDate localDate1 = localDate.plusYears(10);
LocalDate localDate2 = localDate.plusWeeks(8);
LocalDate localDate3 = localDate.plusMonths(1);
LocalDate localDate4 = localDate.plusDays(100);

//调整时间
LocalDate localDate1 = localDate.withDayOfMonth(12);
LocalDate localDate2 = localDate.withMonth(11);
LocalDate localDate3 = localDate.withDayOfYear(100);
LocalDate localDate4 = localDate.withYear(2025);

localdata和date之间的相互转换

// localdate 转为date
 //当前日期
    LocalDate startDate = LocalDate.now();
    //一周后日期
    LocalDate endDate = startDate.plus(1, ChronoUnit.WEEKS);
    //使用LocalDate.atTime方法将日期格式从LocalDate格式转为Date格式
    Instant instant1 = startDate.atTime(LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant();
    Instant instant2 = endDate.atTime(LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant();
    Date startTime = Date.from(instant1);
    Date endTime = Date.from(instant2);


//date转为localdate
        Date date = new Date();
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();

        // atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
        LocalDate localDate = instant.atZone(zoneId).toLocalDate();
        System.out.println("Date = " + date);
        System.out.println("LocalDate = " + localDate);

DateTimeFormatter

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");

SimpleDateFormat

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date= simpleDateFormat.parse("2020-12-12");
String str= simpleDateFormat.format(date);

Calendar

静态方法

//获取一个日历对象
Calendar instance = Calendar.getInstance();

实例方法

//字段值增加
instance.add(Calendar.YEAR,1);
calendar.roll(Calendar.YEAR,2022);

//将某个字段设置为某值
calendar.set(Calendar.YEAR,2022);

//年月日/ 时分/秒
calendar.set(2022,2,1);

//获取某个字段的值
calendar.get(Calendar.MONTH)


//日历之间进行比较
calendar.after(instance) ————Boolean
before   ————Boolean
compareTo  ————int 大于0 小于0 

//获取给定字段的最大值和最小值
getMaximum(int field)
getMinimum(int field) 

// date和calendar 的相互转换
Date date = calendar.getTime();
Calendar calendar = calendar.setTime(date)

fastjson

构造方法

JSONObject jsonObject = new JSONObject();

静态方法

//获取一个JSON对象
 JSONObject jsonObject = JSON.parseObject(json);

//序列化 对象 list集合 map集合
    String s = JSONObject.toJSONString(user);
    
        //枚举常量
        WriteMapNullValue:null
        WriteNullStringAsEmpty:""
        PrettyFormat:格式化输出
        WriteDateUseDateFormat :只有对date才最有效果
        
        // 默认当某个字段的值为空时,不展示该字段
        String s = JSONObject.toJSONString(user, SerializerFeature.WriteMapNullValue);
        {"address":null,"age":20,"id":1,"name":null}
        
        String s = JSONObject.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty);
        {"address":"zz","age":18,"name":""}
    
    String s = JSONObject.toJSONString(users);
    
    String s = JSONObject.toJSONString(map);

// 反序列化
    // 对象
    User user3 = JSON.parseObject(str, User.class);
    //list集合
    List<User> users = JSON.parseArray(jsonString, User.class);
    
    //map集合
    Map<String, User> map = JSON.parseObject(jsonString, new TypeReference<Map<String, User>>(){});
    
    Set<String> set = map.keySet();
    for (String s : set) {
        System.out.println(s);
        System.out.println(map.get(s));
    }

实例方法

// 获取jsonObject的键集合
Set<String> set = jsonObject.keySet();
// 获取值的集合
Collection<Object> values = jsonObject.values();
// 获取键值对的集合
Set<Map.Entry<String, Object>> entries = jsonObject.entrySet();
for (Map.Entry<String, Object> entry : entries) {
    System.out.println(entry.getKey());
    System.out.println(entry.getValue());
}

// 根据键获取值
Object name = jsonObject.get("name");


// 删除指定的键
jsonObject.remove("name");
// 清空所有的键值对
  jsonObject.clear();
  
// 增加属性
jsonObject.put("email", "john@example.com");
jsonObject.putAll(map);

//获取 JSON 对象的元素数量
int size = jsonObject.size();

// 查看json对象是否包含某个值或键
boolean name = jsonObject.containsKey("name");
boolean john = jsonObject.containsValue("John");

注解

@JSonField
该注解作用于方法上、字段上和参数上。可在序列化和反序列化时进行特性功能定制。
 name:序列化后的名字
 ordinal:序列化后的顺序
 format:序列化后的格式
 serialize:是否序列化该字段 ——对象变为字符串时,该属性是否显示
 deserialize:是否反序列化该字段——字符串变为对象时,该属性是否赋值
 serialzeFeatures:序列化时的特性定义

@ JSonType
该注解作用于类上,对该类的字段进行序列化和反序列化时的特性功能定制。
 includes:要被序列化的字段
 orders:序列化后的顺序
 serialzeFeatures:序列化时的特性定义
 
 @JSONType(orders = {"name","address","birth","age"})
 @JSONType(includes = {"name","age"})
                          

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值