文章会有【原创】或者【转载】标示,部分来自Google Baidu的学习结果 {Java/PHP/Python/Ruby/Go}

Java 在学习二十七【原创】  


java.lang.Math //静态方法

java.lang.Date

package coreJava;


import java.text.SimpleDateFormat;

import java.util.Date;


public class MathTest {


public static void main(String[] args) {

// TODO Auto-generated method stub

// Math静态方法

System.out.println(Math.abs(-10));


// 格式化时间

try {

SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");

String createTime = sd.format(new Date());

System.out.println(createTime);

String ss = "2017-06-04";

System.out.println(sd.parse(ss));

} catch (Exception e) {

e.printStackTrace();

}

}


}


java.util.Calendar

//Calendar 本身是抽象类,拿到子类对象

package coreJava;



import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;


public class CalendarTest {


public static void main(String[] args) {

// TODO Auto-generated method stub

Calendar c = Calendar.getInstance();

System.out.println(c.get(Calendar.YEAR));

System.out.println(c.get(Calendar.MONTH) + 1);

System.out.println(c.get(Calendar.DATE));

System.out.println(c.get(Calendar.DAY_OF_MONTH));

System.out.println("###################");

System.out.println("试用期:2016-12-01");

System.out.println("转正:" + getOfficial("2016-12-01"));


        System.out.println("###################");

System.out.println(System.currentTimeMillis());

}

public static String getOfficial(String d) {

if (d.isEmpty()) {

return null;

}

try {

StringBuilder res = new StringBuilder("");

SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");

Date date = sd.parse(d);

Calendar c = Calendar.getInstance();

c.setTime(date);

c.add(Calendar.MONTH, 6);

System.out.println(c.get(Calendar.YEAR));

System.out.println(c.get(Calendar.MONTH) + 1);

System.out.println(c.get(Calendar.DATE));

res.append(c.get(Calendar.YEAR) + "/");

res.append(c.get(Calendar.MONTH) + 1 + "/");

res.append(c.get(Calendar.DATE));

return res.toString();

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return null;

}


}