JAVA数据转换(持续更新中)

package blog;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import com.alibaba.fastjson.JSON;

import edu.huel.blog.model.User;

public class TimeTest {

	public static void main(String[] args) throws ParseException {
		/*----数据转换----*/
		String products="apple,banana,orange";
		//1:字符串转数组
		String[] productArray=products.split(",");
		//2:数组转集合
		List productList=Arrays.asList(",");
		//3:集合转数组
		String foods=StringUtils.join(productList, ",");
		
		/*--JSON数据转换--*/
		String member="{'name':'guanyaqi','sex':'男','home':'henan'}";
		//1.通过JSON.parse把json字符串转换为Map
		Map map=JSON.parseObject(member);
		System.out.println(map);
		//2通过JSON.toJSONString把List转换为json
		List list= new ArrayList<>();
		list.add("apple");
		list.add("banana");
		String jsonString=JSON.toJSONString(list);
		System.out.println("json:"+jsonString);
		//3 JSON字符串转对象
		User user = JSON.parseObject(member, User.class);
		/*List memberList=JSONUtil.parseObject(jsonString, 
					                       new TypeReference>(){});*/
		
		/*----Lambda表达式----*/
		list.forEach(w->{
			System.out.println(w);
		});
		//lambda转集合
		List nums=list.stream().map(
				w->w.toString()).collect(Collectors.toList());
		/*1-分组:
		Map> employeesByCity =
		  employees.stream().collect(groupingBy(Employee::getCity));
		结果:{New York=[Charles], Hong Kong=[Dorothy], London=[Alice, Bob]}
		
		按照城市数量分组:Map numEmployeesByCity =
				  employees.stream().collect(groupingBy(Employee::getCity, counting()));
		{New York=1, Hong Kong=1, London=2}
		
		2-分区:Map> partitioned =
				  employees.stream().collect(partitioningBy(e -> e.getNumSales() > 150));
		结果:{false=[Bob], true=[Alice, Charles, Dorothy]}*/
		
		
		/*----保留两位小数----*/
		 double num=13.154215;  
	    //方式一  
	    DecimalFormat df1 = new DecimalFormat("0.00");  
	    String str = df1.format(num);  
	    System.out.println(str);  //13.15  
	    //方式二  
	    // #.00 表示两位小数 #.0000四位小数  
	    DecimalFormat df2 =new DecimalFormat("#.00");  
	    String str2 =df2.format(num);  
	    System.out.println(str2);  //13.15  
	    //方式三  
	    //%.2f %. 表示 小数点前任意位数   2 表示两位小数 格式后的结果为f 表示浮点型  
	    String result = String.format("%.2f", num);  
	    System.out.println(result);  //13.15  
		
	    /*----BigDecimal用法----*/ 
	    BigDecimal sum_amount=BigDecimal.ZERO;
	    BigDecimal b1 = new BigDecimal("28.0109");  
	    System.out.println("小数格式化:" + df1.format(b1)); 
	    //BigDecimal转DOuble
	    Double b2=b1.doubleValue();
	    
	    /*----日期转换----*/
	    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	    Date date=new Date();
	    String dateString="2019-01-12";
	    System.out.println(date);
	    //format需要的参数是Date格式的
	    System.out.println(df.format(date));
	    //df.parse将字符换格式转换成Date格式
	    System.out.println(df.format(df.parse(dateString)));
	    
	    //DateUtils.parseDate将String类型转换成Date格式
	    Date dateTime=DateUtils.parseDate(dateString, "YYYY-MM-DD");
	    System.out.println(dateTime);
	    //DateFormatUtils.format() 将Date转String
	    String time=DateFormatUtils.format(new Date(), "YYYY-MM-DD HH:mm:ss");
	    System.out.println(time);
	    
	    //日历Calender
	    Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        //比当前月份少1
        int month = cal.get(Calendar.MONTH);
        //date表示日期,day表示天数,所以date与day_of_month相同
        int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
        //表示本周的第几天,从周日开始计算
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        int dayOfYear = cal.get(Calendar.DAY_OF_YEAR);
        //12小时制
        int hour = cal.get(Calendar.HOUR);
        //24小时制
        int hourOfDay = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        int millisecond = cal.get(Calendar.MILLISECOND);
        int maxDate = cal.getActualMaximum(Calendar.DATE);
        System.out.println("现在的年份为:" + year);
        System.out.println("现在的月份为:" + month);
        System.out.println("现在的号为:" + dayOfMonth);
        System.out.println("现在是星期:" + dayOfWeek);
        System.out.println("现在过了的天数为:" + dayOfYear);
        System.out.println("现在几点:" + hour);
        System.out.println("现在几点:" + hourOfDay);
        System.out.println("现在几分:" + minute);
        System.out.println("现在几秒:" + second);
        System.out.println("现在几毫秒:" + millisecond);
        System.out.println("本月最后一天是:" + maxDate);
	    
	    
	    
		
		
	}
}

 

你可能感兴趣的:(Java)