zoukankan      html  css  js  c++  java
  • Java Day 20

    System类
     long currentTimeMillis();当前时间毫秒值
     
     getProperties()
      获取系统的属性信息,存储到properties集合
      使用自身的方法存取元素
      也可以自己定义一些属性信息

    Runtime类
     Runtime 没有构造方法,不可以创建对象
     有非静态方法,则该类应该提供静态的返回该类对象的方法。
     单例设计模式

    Math类
     final修饰
     ceil()
     floor()
     round()

    Date类
     毫秒与日期转化
     
     日期对象转换成日期格式字符串
        DateFormat类中的format方法

     日期格式字符串转换成日期对象
        DateFormat类中的parse方法

      1 package com.company.Day020;
      2 
      3 import com.oracle.webservices.internal.api.message.PropertySet;
      4 import org.omg.Messaging.SYNC_WITH_TRANSPORT;
      5 
      6 import java.io.IOException;
      7 import java.text.DateFormat;
      8 import java.text.ParseException;
      9 import java.text.SimpleDateFormat;
     10 import java.util.Calendar;
     11 import java.util.Date;
     12 import java.util.Properties;
     13 import java.util.Set;
     14 
     15 /**
     16  * Created by junius on 2016/10/16.
     17  */
     18 public class Demo001 {
     19     private static final String LINE_SEPARATOR = System.getProperty("line.separator");
     20 
     21     public static void main(String[] args) throws ParseException {
     22         //System.out.println(System.currentTimeMillis());
     23         //demo_1();
     24        // System.out.print("hello"+LINE_SEPARATOR+"world");
     25 //        demo_3();
     26         String s1 = "2016-9-12";
     27         String s2 = "2016-9-16";
     28         int days = getDays(s1,s2);
     29         //System.out.print("days = "+days);
     30         int year =2012;
     31         showDate2(year);
     32 
     33 
     34     }
     35 
     36     public static void showDate2(int year) {
     37         Calendar c = Calendar.getInstance();
     38         c.set(year,2,1);
     39         c.add(Calendar.DAY_OF_MONTH,-1);
     40         showDate(c);
     41     }
     42 
     43     private static void showDate(Calendar c) {
     44         int year = c.get(Calendar.YEAR);
     45         int month = c.get(Calendar.MONTH)+1;
     46         int day = c.get(Calendar.DAY_OF_MONTH);
     47         int week = c.get(Calendar.DAY_OF_WEEK);
     48         System.out.print(year+"/"+month+"/"+day+" "+getWeek(week));
     49     }
     50 
     51     private static String getWeek(int i) {
     52         String[] week ={"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
     53         return week[i];
     54     }
     55 
     56     private static int getDays(String s, String s1) throws ParseException {
     57         DateFormat dateFormat = DateFormat.getDateInstance();
     58         dateFormat = new SimpleDateFormat("yyyy-MM-dd");
     59 
     60         Date date_Str1 =dateFormat.parse(s);
     61         Date date_Str2 =dateFormat.parse(s1);
     62 
     63         long l1 = date_Str1.getTime();
     64         long l2 = date_Str2.getTime();
     65 
     66         long time = Math.abs(l1-l2);
     67 
     68         int day = getDay(time);
     69         return day;
     70     }
     71 
     72     private static int getDay(long time) {
     73         return (int)(time/1000/60/60/24);
     74     }
     75 
     76     private static void demo_3() throws ParseException {
     77         String str_date = "2016--10--16";
     78         DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
     79         dateFormat = new SimpleDateFormat("yyyy--MM--dd");
     80         Date date = dateFormat.parse(str_date);
     81 
     82         System.out.println(date);
     83     }
     84 
     85     private static void demo_2() {
     86         Date date = new Date();
     87         DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
     88         //dateFormat = DateFormat.getDateTimeInstance();
     89         String str_date = dateFormat.format(date);
     90         System.out.print(str_date);
     91     }
     92 
     93     private static void demo_1() {
     94 
     95         Runtime r = Runtime.getRuntime();
     96         try {
     97             r.exec("notepad.exe");
     98         } catch (IOException e) {
     99             e.printStackTrace();
    100         }
    101 
    102         Properties pp = System.getProperties();
    103         Set<String> nameset = pp.stringPropertyNames();
    104 
    105         for(String name : nameset){
    106             String value = pp.getProperty(name);
    107             System.out.println(name+":"+value);
    108         }
    109     }
    110 
    111 
    112 }

    Calendar类

    IO流
     字节流的抽象基类
     InputStream OutputStream
     字符流的抽象基类
     Reader Writer
     
     文字数据,优先考虑字符流
     如果文件不存在,就自动创建
     如果文件存在,就覆盖。

     换行和续写
      续写:使用构造函数
     

     1 package com.company.Day020;
     2 
     3 import java.io.FileWriter;
     4 import java.io.IOException;
     5 
     6 /**
     7  * Created by junius on 2016/10/16.
     8  */
     9 public class File002{
    10     private static String LINE_SEPARATOR=System.getProperty("line.separator");
    11 
    12     public static void main(String[] args){
    13         FileWriter fw = null;
    14         try {
    15             fw = new FileWriter("demo.txt");
    16             fw.write("ww"+LINE_SEPARATOR+"w");
    17             fw.flush();
    18             fw.write("zz
    z");
    19         } catch (IOException e) {
    20             e.printStackTrace();
    21         }finally {
    22             if(fw!=null)//如果不判断就会报空指针异常
    23             try {
    24                 fw.close();
    25             } catch (IOException e) {
    26                 e.printStackTrace();
    27             }
    28         }
    29 
    30 
    31     }
    32 }


     IO异常处理
     
     FileReader -- 读取方式一
      1、确保文件一定存在

     读取方式二
      read(char[])
     

     1 package com.company.Day020;
     2 
     3 import java.io.FileNotFoundException;
     4 import java.io.FileReader;
     5 import java.io.IOException;
     6 import java.io.Reader;
     7 
     8 /**
     9  * Created by junius on 2016/10/17.
    10  */
    11 public class ReadDemo002 {
    12     public static void main(String[] args) throws IOException {
    13         Reader r = null;
    14         int i=0;
    15 
    16         r = new FileReader("demo.txt");
    17 
    18         while((i=r.read())!=-1){
    19             //System.out.print((char)i);
    20         }
    21 
    22         r.close();
    23 
    24         Reader r2 = new FileReader("demo.txt");
    25 
    26         char[] buf = new char[10];
    27 
    28         //int num = r2.read(buf);
    29 
    30         int len = 0;
    31         while((len=r2.read(buf))!=-1){
    32             System.out.print(new String(buf,0,len));
    33         }
    34 
    35 
    36     }
    37 }


     
     

  • 相关阅读:
    PAT:1002. 写出这个数 (20) AC
    PAT:1031. 查验身份证(15) AC
    PAT:1021. 个位数统计 (15) AC
    NSDate
    iOS程序的生命的周期
    Swift swith语句
    Swift 循环语句
    Swift 基本运算符
    Swift 解包
    Swift 可选类型(补充)
  • 原文地址:https://www.cnblogs.com/zhuzhuqwa/p/5971700.html
Copyright © 2011-2022 走看看