JAVA:
1、map 存取方法
2、取list所有元素方式
3、是否了解json对象,map怎么转换为jsonobject,java数组和list怎么转换成jsonarray,
下面这段代码的功能:
operAge: function (birth,nowdate){
var age;
var nowDateFormat = nowdate.replace(/-/g,"");
var birthFormat = birth.replace(/-/g,"");
var years = parseInt(nowDateFormat.substr(0,4),10);
var month = parseInt(nowDateFormat.substr(4,2),10);
var days = parseInt(nowDateFormat.substr(6,2),10);
var birthYear = parseInt(birthFormat.substr(0,4),10);
var birthMonth = parseInt(birthFormat.substr(4,2),10);
var birthDate = parseInt(birthFormat.substr(6,2),10);
if(month-birthMonth {
age = yeas- birthYear -1;
}
else if(month-birthMonth>0)
{
age = yeas- birthYear;
}
else
{if(days-birthDate>=0)
{age = years - birthYear;}
else
{age = years - birthYear-1;}
}
if (age<0) age = 0;
return age;
}
下面这段代码功能是什么?
publisc final static String TimeUnit_YEAR = "year";
publisc final static String TimeUnit_MONTH = "month";
publisc final static String TimeUnit_DAY = "day";
publisc static Date accountDate(Date givenDate,int amount,String unit)
{
Calendar c = Calendar.getInstance();
c.setTime(giveDate);
if(unit != null && unit.trime().equals(TimeUnit_DAY))
{c.add(Calendar.DATE,amount);
return c.getTime();}
else if(unit != null && unit.trime().equals(TimeUnit_MONTH))
{c.add(Calendar.MONTH,amount);
return c.getTime();}
else if(unit != null && unit.trime().equals(TimeUnit_YEAR))
{c.add(Calendar.YEAR,amount);
return c.getTime();}
else
{return givenDate;}
}
前三个问题google下就有。第四个代码,从字符串中解析生日,并且计算年龄。最后一个代码,给定日期、单位、增量,计算得到另一个日期。
下面这段代码功能是什么?其实就是根据传递的添加参数的类型,给指定的时间上加上指定数量amount的日期值,。
java对象与json之间的转换,我觉得最好用的是 阿里巴巴的 fastjson
详细参考 http://blog.csdn.net/wilsonke/article/details/37921571
1)map存取方法:map中放值,map是key-value的形式存放的.就好比一个钥匙对应一个钥匙孔;如:
Map map = new HashMap();
map.put(“1”,“男”);map.put(“0”,“女”);
从map中取值:String str = map.get(”1”).toString();结果是:str = ”男”;
遍历一个map,从中取得key 和value
Map map = new HashMap() ;
Iterator it = map.entrySet().iterator() ;
while (it.hasNext())
{
Map.Entry entry = (Map.Entry) it.next() ;
Object key = entry.getKey() ;
Object value = entry.getValue() ;
}
2)取list所有元素方式
List list = new ArrayList();
//添加元素:
list.add(1);
list.add(2);
list.add(3);
for (int i = 0; i < list.size(); i++) {
// 取出元素:list.get(index);
System.out.println(list.get(i));
}
list 还有其他方法可以自己动手学习一下。
3)json对象
json字符串是json对象的string形式,也就是string,但是符合json对象的格式
若是json对象,那就可以使用getXxx(..)来得到某key对应的value了。
json格式就说白了就是键值对
比如
String jsonStr = "{name:\"lisi\", age:18}";
//json字符串就转换成了一个json对象
JSONObject json = JSONObject.fromObject(jsonStr);
4)常用的json 转换工具类
有阿里巴巴的 fastjson,还有就是google的 gson;
至于怎么用就自己学习下,给你个参考http://blog.csdn.net/a249900679/article/details/51386660
5)代码功能
第一段代码是通过出生年月日和当前系统时间计算年龄大小。是用js 写的。
第二段代码是对日历的处理。是用java写的。
纯手打的,回答得有点粗糙,希望能帮到你。
@ifnaer说得很详细了