其他API

来源:互联网 发布:淘宝客api视频教程 编辑:程序博客网 时间:2024/06/06 20:24

API-System

import java.util.Properties;import java.util.Set;public class SystemDemo {    private static final String LINE_SEPARATOR = System.getProperty("line.separator");    public static void main(String[] args) {        /*         * System:         * 1,不需要实例化,都是静态的属性和方法。         * 2,out对应标准输出流(显示器),int属性对应的是键盘。         * 演示一些System类中的方法。         * currentTimeMilles:获取当前时间。可以用于计算程序运行时间只要将开始时间和结束时间相减即可。         */        long time = System.currentTimeMillis();        System.out.println(time);//毫秒值。1382691495296        //演示getProperties()获取系统属性集。        Properties prop = System.getProperties();        //获取系统属性集中的信息,遍历Properties集合。使用map的方法没问题,但是map有泛型取出时要强转。        //Properties有没有提供自身获取数据的方法呢?        //获取键集合。//      Set<String> keySet = prop.stringPropertyNames();//      for(String key : keySet){//          String value = prop.getProperty(key);//通过键获取值。//          System.out.println(key+"::::"+value);//      }        //获取指定信息,比如:操作系统。        String osname =System.getProperty("os.name");        System.out.println(osname);        //获取系统中的行分隔符。这样该程序在移植时,很方便。不同的系统,获取该系统上行分隔符        System.out.println("hello"+LINE_SEPARATOR+"itcast");    }}

API-Math

import java.util.Random;public class MathDemo {    public static void main(String[] args) {        /*         * Math数序运算。方法都是静态的。         * Math.PI         *///      Math.abs(-4);        double d1 = Math.ceil(-12.34); //获取参数右边的整数   //11 12floor 12.34  ceil13  14  15        double d2 = Math.floor(12.34);//获取参数左边的整数。        double d3 = Math.round(12.54);//四舍五入。//      System.out.println("d1="+d1);//13   //      System.out.println("d2="+d2);//12//      System.out.println("d3="+d3);//      System.out.println(Math.pow(10,3));        Random r = new Random();        for(int x=0; x<10; x++){//          int d = (int)(Math.random()*6 + 1);//          double d = Math.ceil(Math.random()*6);            int num  = r.nextInt(6)+1;            System.out.println(num);        }                   }}

将毫秒值转成本地格式-Date DateFormat

import java.text.DateFormat;import java.util.Date;public class DateDemo {    public static void main(String[] args) {        long time = System.currentTimeMillis();//      System.out.println(time);//1382837734218        //【怎么能将这个时间转成我们所熟悉的时间呢?】        //通过另请参阅找到Date类。发现日期对象一初始化可以传递一个毫秒值。        time = 1382837734218l;        //创建了一个日期对象,将已有的毫秒值进行封装。通过日期对象的方法获取其中的相关信息。比如年月日。        Date date = new Date(time);        System.out.println(date.toString());//Sun Oct 27 09:35:34 CST 2013        //对象的方法已过时。通过toString获取了国际化的信息。如何让信息本土化。        //toLocaleString() 已过时,通过查阅信息发现替代从 JDK 1.1 开始,由 DateFormat.format(Date date) 取代。        //要格式化一个当前语言环境下的日期,可使用某个静态工厂方法:myString = DateFormat.getDateInstance().format(myDate);        //在获取格式器对象时可以明确风格。 FULL、LONG、MEDIUM(默认风格)和 SHORT        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);        dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);        //调用format方法对日期对象进行格式化。用默认风格。        String str_date = dateFormat.format(date);        System.out.println(str_date);    }}

获取日期中的指定日期信息比如获取年,月,日Calendar。

import java.util.Calendar;public class DateDemo2 {    public static void main(String[] args) {        //需求2:基于需求1,获取到了日期和时间的字符串信息(本地)。        //如何获取字符串中指定的日期信息呢?比如获取年,并判断。        //1,获取日期对象。Date//      Date date = new Date();        //jdk1.1开始 Calendar。//获取日历对象。        Calendar c = Calendar.getInstance();//        int year = c.get(Calendar.YEAR);        int month = c.get(Calendar.MONTH)+1;        int day = c.get(Calendar.DAY_OF_MONTH);        String week = getCnWeek(c.get(Calendar.DAY_OF_WEEK));        //打印信息中年月日等相关信息都在这里。获取指定字段的值就哦了。        System.out.println(year+"年"+month+"月"+day+"日  "+week);    }    //让星期字段对应的中文的星期。查表。数组。    public static String getCnWeek(int i) {        if(i<0 || i>7){            throw new RuntimeException(i+"没有对应的星期");        }        //定义表。        String[] weeks = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};        return weeks[i];    }}/*java.util.GregorianCalendar[time=1382840743546,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2013,MONTH=9,WEEK_OF_YEAR=44,WEEK_OF_MONTH=5,DAY_OF_MONTH=27,DAY_OF_YEAR=300,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=10,HOUR_OF_DAY=10,MINUTE=25,SECOND=43,MILLISECOND=546,ZONE_OFFSET=28800000,DST_OFFSET=0]*/

“2012-3-17”转成日期对象

import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class DateTest {    /**     * @param args     * @throws ParseException      */    public static void main(String[] args) throws ParseException {//      练习2 :"2012-3-17"转成日期对象。        /*         * 之前有做过,将一个日期对象转成日期文本字符串这个称之为格式化。         * 现在要做的是文本--->日期对象:解析。         * 这些都是DateFormat中的功能。         *          * 不同日期风格的文本对应不同的格式器。         */        String str_date = "2012年3月17日";        //日期格式器。DateFormat        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);        Date date = dateFormat.parse(str_date);        System.out.println(date);        //这种格式在给定的四种风格中不存在。怎么解析呢?只能使用子类SimpleDateFormat的方法,        //1,构造函数明确特定风格,2,applyPattern 方法来修改格式模式        str_date = "2013/3/17 17--13--45";        DateFormat dateFormat2 = new SimpleDateFormat("yyyy/MM/dd hh--mm--ss");        Date date2 = dateFormat2.parse(str_date);        System.out.println(date2);    }}

“2013-4-25”到”2013年7月29日”到底有多少天?

import java.text.DateFormat;import java.text.ParseException;import java.util.Date;public class DateTest2 {    /**     * @param args     * @throws ParseException      */    public static void main(String[] args) throws ParseException {//      练习3,"2013-4-25"到"2013年7月29日"到底有多少天?        /*         * 思路:         * 1,到底有多少天?相减的过程。         * 2,字符串也不能相减啊,毫秒值可以相减。         * 3,怎么获取毫秒值呢?毫秒值-->日期对象, 日期对象-->毫秒值。         * 4,怎么获取日期对象呢?需要将字符串文本--解析-->日期对象。         */        String str_date1 = "2013-4-25";        String str_date2 = "2013年7月29日";        //需要定义两个模式。一个解析str_date1,一个解析str_date2。        int style_1 = DateFormat.MEDIUM;//默认风格。        int style_2 = DateFormat.LONG;//默认风格。        int days = getDays(str_date1,str_date2, style_1,style_2);        System.out.println("days="+days);    }    private static int getDays(String str_date1, String str_date2,            int style_1, int style_2) throws ParseException {        //1,根据给定风格创建格式器对象。        DateFormat format_1 = DateFormat.getDateInstance(style_1);        DateFormat format_2 = DateFormat.getDateInstance(style_2);        //2,对文本进行解析。        Date date_1 = format_1.parse(str_date1);        Date date_2 = format_2.parse(str_date2);        //3,获取日期对象毫秒值。        long time_1 = date_1.getTime();        long time_2 = date_2.getTime();        //4,相减。        long time = Math.abs(time_1 - time_2);//      System.out.println(time);        int day = (int)(time/1000/60/60/24);        return day;    }}

获取给定年份的2月有多少天?【面试题】

import java.util.Calendar;public class DateTest3 {    public static void main(String[] args) {        // 4,获取给定年份的2月有多少天?【面试题】        for (int year = 2000; year <= 2020; year++) {            show(year);        }    }    public static void show(int year) {        Calendar c = Calendar.getInstance();//        // 有获取有设置 set        // c.set(Calendar.YEAR, 2011);        c.set(year, 2, 1);        // 时间是连续的,3月1日的前一天就2月的最后一天,知道2月份的天数。        c.add(Calendar.DAY_OF_MONTH, -1);        int year1 = c.get(Calendar.YEAR);        int month = c.get(Calendar.MONTH) + 1;        int day = c.get(Calendar.DAY_OF_MONTH);        String week = getCnWeek(c.get(Calendar.DAY_OF_WEEK));        // 打印信息中年月日等相关信息都在这里。获取指定字段的值就哦了。        System.out.println(year1 + "年" + month + "月" + day + "日  " + week);    }    public static String getCnWeek(int i) {        if (i < 0 || i > 7) {            throw new RuntimeException(i + "没有对应的星期");        }        // 定义表。        String[] weeks = { "", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };        return weeks[i];    }}
原创粉丝点击