【学習ノート】java基本タイプ変換と日付操作およびMath

8442 ワード

一.基本タイプ変換
Javaではすべてオブジェクトですが、基本的なデータ型はオブジェクトではありません.したがって、JDKでは基本データ型のカプセル化クラスオブジェクトが提供される.8つの基本データ型に対応するパッケージクラス(wrapper class):boolean  -->  Booleanchar  -->  Characterbyte  -->  Byteshort  --->  Shortint  -->  Integerlong  -->  Longfloat  -->  Floatdouble  -->  Doubleの8つの基本データ型は、ObjectのhashCode()、equals()、toString()メソッドを書き換えています.
基本タイプ----->パッケージタイプ Integer inObj = new Integer(10); System.out.println(inObj);パッケージタイプ----->基本タイプ int i = inObj.intValue(); System.out.println(i);基本タイプ------>文字列 String stri = String.valueOf(i); System.out.println(stri);文字列------>基本タイプ int inti = Integer.parseInt("100"); System.out.println(inti);
例:
package com.fjnu.study;



public class TestChange {

    public static void main(String[] args){

        int i;

        double b = 123.123;

        

        Integer int1 = new Integer(10);

        System.out.println(int1);

        System.out.println(Integer.toBinaryString(3));

        

    }

}
10

11

 
doubleを文字列に変換
package com.fjnu.study;



public class TestChange {

    public static void main(String[] args){

        double i = 1234.344;

        String str = String.valueOf(i);

        System.out.println(str);

        System.out.println(str.length());

    }

}
1234.344

8

 
文字列をint型に変換
package com.fjnu.study;



public class TestChange {

    public static void main(String[] args){

        String str = "12345";

        int i = Integer.parseInt(str);

        System.out.println(str);

    }

}
12345

Characterメソッド
常用方法:static boolean isDigit(char ch)//デジタルstatic boolean isLetter(char ch)static boolean isLetterOrDigit(char ch)  static boolean isLowerCase(char ch) static boolean isUpperCase(char ch) static char toLowerCase(char ch)  static char toUpperCase(char ch)   static boolean isSpaceChar(char ch) String toString()
 
二.システム時間を取得する方法
Systemクラスを使用java.util.Dateクラスを使用java.util.Calendarクラスを使用
1.Systemクラスの使用
ドキュメントを参照:
currentTimeMillis



public static long currentTimeMillis()

             。  ,             ,             ,        。  ,                  。

    Date     ,       “     ”      (UTC)          。



  :

           1970   1   1          (        )。

2.java.util.Dateクラスの使用
システム時間を取得するために使用され、中のTimeはデータベース時間を処理するために使用されます.
 package com.fjnu.study;

 import java.util.Date;

 

public class TestTime {

    public static void main(String[] args){

        Date date = new Date();

        System.out.println(date);

    }

}
Sat Apr 11 11:26:16 CST 2015

3.java.util.Calendarクラスを使用
Calendarクラスは、カレンダーを記述する抽象クラスです.このクラスは直接初期化できませんが、Calendarオブジェクトを作成するためのクラスメソッドgetInstance()があります.一般的な方法:static Calendar getInstance()int get(int field);Date getTime()   int get(int field);void set(int field, int value);void setTime(Date date);void setTimeInMillis(long millis);
例:
 package com.fjnu.study;

 import java.util.Date;

 import java.util.Calendar;

 

public class TestTime {

    public static void main(String[] args){

        TestTime.CalendarMethod();

    }

    private static void CalendarMethod(){

        Calendar calendar = Calendar.getInstance();

        System.out.println(calendar);

        System.out.println(calendar.getTime());

        System.out.println(calendar.get(calendar.YEAR));

    }

}
java.util.GregorianCalendar[time=1428723507803,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=2015,MONTH=3,WEEK_OF_YEAR=15,WEEK_OF_MONTH=2,DAY_OF_MONTH=11,DAY_OF_YEAR=101,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=11,HOUR_OF_DAY=11,MINUTE=38,SECOND=27,MILLISECOND=803,ZONE_OFFSET=28800000,DST_OFFSET=0]

Sat Apr 11 11:38:27 CST 2015

2015

注意:System.out.println(calendar.get(calendar.YEAR);ここのgetは取得フィールドで、ここでcalendar.YEARを使うことができなくて、いいえ者の出した結果は1です
 
三.Math
一般的な方法:static double abs(double a);//リロード方法static double ceil(double a);//リロード方法static double floor(double a);//リロード方法static long round(double a);//リロード方法static double pow(double a,double b);static double random();
package com.fjnu.study;



public class TestMath {

    public static void main(String[] args){

        one();

    }

    

    private static void one(){

        System.out.println(Math.max(Math.max(1,30), 50));

    }

}

小数点以下の操作:
Math.roundMath.ceilMath.floorMath.random
 
乱数を生成
Math.random();Randomオブジェクトの使用
 
重要点:BigDecimalアクション