时间操作
public static void main(String[] args){
//设定时间格式
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar=Calendar.getInstance();
//当前时间加1天
calendar.setTime(new Date());
calendar.add(Calendar.DATE,1);
String biz_time=simpleDateFormat.format(calendar.getTime());
String gmtcreatetime=String.valueOf(calendar.getTimeInMillis()); //转换成毫秒时间戳
String biz_time12=simpleDateFormat.format(Double.parseDouble(gmtcreatetime)); //时间戳转换成毫秒时间
System.out.println(biz_time);
System.out.println(gmtcreatetime);
//当前时间减1天
calendar.setTime(new Date());
calendar.add(Calendar.DATE,-1);
String biz_time1=simpleDateFormat.format(calendar.getTime());
String gmtcreatetime1=String.valueOf(calendar.getTimeInMillis());//转换成毫秒时间戳
System.out.println(biz_time1);
System.out.println(gmtcreatetime1);
//当前时间上下一个月
calendar.setTime(new Date());
calendar.add(Calendar.MONTH,1);
String biz_time2=simpleDateFormat.format(calendar.getTime());
String gmtcreatetime2=String.valueOf(calendar.getTimeInMillis());//转换成毫秒时间戳
System.out.println(biz_time2);
System.out.println(gmtcreatetime2);
calendar.setTime(new Date());
calendar.add(Calendar.MONTH,-1);
String biz_time3=simpleDateFormat.format(calendar.getTime());
String gmtcreatetime3=String.valueOf(calendar.getTimeInMillis());//转换成毫秒时间戳
System.out.println(biz_time3);
System.out.println(gmtcreatetime3);
//当前时间上下一年
calendar.setTime(new Date());
calendar.add(Calendar.YEAR,1);
String biz_time4=simpleDateFormat.format(calendar.getTime());
String gmtcreatetime4=String.valueOf(calendar.getTimeInMillis());//转换成毫秒时间戳
System.out.println(biz_time4);
System.out.println(gmtcreatetime4);
calendar.setTime(new Date());
calendar.add(Calendar.YEAR,-1);
String biz_time5=simpleDateFormat.format(calendar.getTime());
String gmtcreatetime5=String.valueOf(calendar.getTimeInMillis());//转换成毫秒时间戳
System.out.println(biz_time5);
System.out.println(gmtcreatetime5);
//获取当前时间
System.out.println(simpleDateFormat.format(new Date()));
/**随机操作**/
System.out.println(RandomStringUtils.randomAlphanumeric(15));
}
二、成员方法
Calendar类的成员方法
static Calendar getInstance() | 使用默认时区和区域设置获取日历。通过该方法生成Calendar对象。如下所示:Calendar cr=Calendar.getInstance(); |
---|---|
public void set(int year,int month,int date,int hourofday,int minute,int second) | 设置日历的年、月、日、时、分、秒。 |
public int get(int field) | 返回给定日历字段的值。所谓字段就是年、月、日等等。 |
public void setTime(Date date) | 使用给定的Date设置此日历的时间。Date———Calendar |
public Date getTime() | 返回一个Date表示此日历的时间。Calendar——-Date |
abstract void add(int field,int amount) | 按照日历的规则,给指定字段添加或减少时间量。 |
public long getTimeInMillies() | 以毫秒为单位返回该日历的时间值。 |
三、日历字段
日历字段包含以下两种:一种是表示时间的单位,例如年、月、日等等。另一种是具体的日期,例如一月、二月、三月、一日、二日、三日、一点钟、两点钟等等具体的时间。前一种一般时获取的时候使用,后一种一般判断的时候使用。
时间单位字段:
YEAR | 年 | MINUTE | 分 | DAY_OF_WEEK_IN_MONTH | 某月中第几周 |
---|---|---|---|---|---|
MONTH | 月 | SECOND/MILLISECOND | 秒/毫秒 | WEEK_OF_MONTH | 日历式的第几周 |
DATE | 日 | DAY_OF_MONTH | 和DATE一样 | DAY_OF_YEAR | 一年的第多少天 |
HOUR_OF_DAY | 时 | DAY_OF_WEEK | 周几 | WEEK_OF_YEAR | 一年的第多少周 |
具体时间字段:这里就不累赘了,从一月到十二月、从周一到周五
特殊的:AM_PM 返回1则表示是下午,返回0表示上午。
随机操作
1.第一种 生成的字符串每个位置都有可能是str中的一个字母或数字,需要导入的包是import java.util.Random;
1. //length用户要求产生字符串的长度
2. public static String getRandomString(int length){
3. String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
4. Random random=new Random();
5. StringBuffer sb=new StringBuffer();
6. for(int i=0;i<length;i++){
7. int number=random.nextInt(62);
8. sb.append(str.charAt(number));
9. }
10. return sb.toString();
11. }
2.第二种 可以指定某个位置是a-z、A-Z或是0-9,需要导入的包是import java.util.Random;
1. //可以指定字符串的某个位置是什么范围的值
2. public static String getRandomString2(int length){
3. Random random=new Random();
4. StringBuffer sb=new StringBuffer();
5. for(int i=0;i<length;i++){
6. int number=random.nextInt(3);
7. long result=0;
8. switch(number){
9. case 0:
10. result=Math.round(Math.random()*25+65);
11. sb.append(String.valueOf((char)result));
12. break;
13. case 1:
14. result=Math.round(Math.random()*25+97);
15. sb.append(String.valueOf((char)result));
16. break;
17. case 2:
18. sb.append(String.valueOf(new Random().nextInt(10)));
19. break;
20. }
21.
22.
23. }
24. return sb.toString();
25. }
3.第三种 org.apache.commons.lang包下有一个RandomStringUtils类,其中有一个randomAlphanumeric(int length)函数,可以随机生成一个长度为length的字符串。
//产生5位长度的随机字符串,中文环境下是乱码
RandomStringUtils.random(5);
//使用指定的字符生成5位长度的随机字符串
RandomStringUtils.random(5, new char[]{‘a’,’b’,’c’,’d’,’e’,’f’, ‘1’, ‘2’, ‘3’});
//生成指定长度的字母和数字的随机组合字符串
RandomStringUtils.randomAlphanumeric(5);
//生成随机数字字符串
RandomStringUtils.randomNumeric(5);
//生成随机[a-z]字符串,包含大小写
RandomStringUtils.randomAlphabetic(5);
//生成从ASCII 32到126组成的随机字符串
RandomStringUtils.randomAscii(4)
拼接字符串
SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("yyyyMMdd");
Calendar calendar2=Calendar.getInstance();
calendar2.setTime(new Date());
calendar2.add(Calendar.DATE,1);
String biz_time2=simpleDateFormat1.format(calendar2.getTime());
List<String> arrays= Arrays.asList("METRO","|APY",biz_time2,"193417772071040NPS");
CharSequence[] charSequences= arrays.toArray(new CharSequence[arrays.size()]);
String s=String.join("", charSequences);
jsonObject.getJSONObject("promo_info").put("business_principal_id",s);
参数处理—json
public static void main(String[] args){
// Java里的json数据
String jsonObject = "{\"code\":\"10000\",\"charge\":false,\"msg\":\"查询成功\",\"result\":{\"HeWeather5\":[{\"daily_forecast\":[{\"astro\":{\"mr\":\"11:34\",\"ms\":\"22:36\",\"sr\":\"06:57\",\"ss\":\"16:17\"},\"cond\":{\"code_d\":\"100\",\"code_n\":\"100\",\"txt_d\":\"晴\",\"txt_n\":\"晴\"},\"date\":\"2018-12-14\",\"hum\":\"46\",\"pcpn\":\"0.0\",\"pop\":\"0\",\"pres\":\"1039\",\"tmp\":{\"max\":\"-2\",\"min\":\"-10\"}}],\"status\":\"ok\"}]}}";
JSONObject jsonObject1= JSONObject.parseObject(jsonObject);
System.out.println(jsonObject1);
//获取某一个键的值
String a=jsonObject1.getString("msg");
System.out.println(a);
//获取某一个布尔型的值
Boolean b=jsonObject1.getBoolean("charge");
System.out.println(b);
//获取json中某一个对象
JSONObject c=jsonObject1.getJSONObject("result");
System.out.println(c);
//获取对象中的某一个list
JSONArray d=c.getJSONArray("HeWeather5");
System.out.println(d);
//获取列表对象中第一个下表的值
JSONObject e=d.getJSONObject(0);
System.out.println(e);
//总体思路,如果value是string就getString 如果是list 就getJSONArray,对象就getJSONObject,布尔型就getBoolean
// System.out.println(StringjsonObject1.getJSONObject("msg"));
}
总体思路,如果value是string就getString 如果是list 就getJSONArray,对象就getJSONObject,布尔型就getBoolean
FastJson输出null值
https://github.com/alibaba/fastjson/wiki/WriteNull_cn
SerializerFeature | 描述 |
---|---|
WriteNullListAsEmpty | 将Collection类型字段的字段空值输出为[] |
WriteNullStringAsEmpty | 将字符串类型字段的空值输出为空字符串 “” |
WriteNullNumberAsZero | 将数值类型字段的空值输出为0 |
WriteNullBooleanAsFalse | 将Boolean类型字段的空值输出为false |
属性 | 备注 |
---|---|
QuoteFieldNames | 输出key时是否使用双引号,默认为true |
UseSingleQuotes | 使用单引号而不是双引号,默认为false |
WriteMapNullValue | 是否输出值为null的字段,默认为false |
WriteEnumUsingToString | Enum输出name()或者original,默认为false |
UseISO8601DateFormat | Date使用ISO8601格式输出,默认为false |
WriteNullListAsEmpty | List字段如果为null,输出为[],而非null |
WriteNullStringAsEmpty | 字符类型字段如果为null,输出为”“,而非null |
WriteNullNumberAsZero | 数值字段如果为null,输出为0,而非null |
WriteNullBooleanAsFalse | Boolean字段如果为null,输出为false,而非null |
SkipTransientField | 如果是true,类中的Get方法对应的Field是transient,序列化时将会被忽略。默认为true |
SortField | 按字段名称排序后输出,默认为false |
WriteTabAsSpecial | 把\t做转义输出,默认为false 不推荐 |
PrettyFormat | 结果是否格式化,默认为false |
WriteClassName | 序列化时写入类型信息,默认为false。反序列化是需用到 |
DisableCircularReferenceDetect | 消除对同一对象循环引用的问题,默认为false |
WriteSlashAsSpecial | 对斜杠’/’进行转义 |
BrowserCompatible | 将中文都会序列化为\uXXXX格式,字节数会多一些,但是能兼容IE 6,默认为false |
WriteDateUseDateFormat | 全局修改日期格式,默认为false。JSON.DEFFAULT_DATE_FORMAT = “yyyy-MM-dd”;JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat); |
DisableCheckSpecialChar | 一个对象的字符串属性中如果有特殊字符如双引号,将会在转成json时带有反斜杠转移符。 如果不需要转义,可以使用这个属性,默认为false |
NotWriteRootClassName | |
BeanToArray | 将对象转为array输出 |
WriteNonStringKeyAsString | |
NotWriteDefaultValue | |
BrowserSecure |
Model obj = ...;
JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue);
class Model {
public List<Objec> items;
}
Model obj = ....;
String text = JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty);