本文最后更新于:January 20, 2022 am
积土成山,风雨兴焉;积水成渊,蛟龙生焉;积善成德,而神明自得,圣心备焉。故不积跬步,无以至千里,不积小流无以成江海。齐骥一跃,不能十步,驽马十驾,功不在舍。面对悬崖峭壁,一百年也看不出一条裂缝来,但用斧凿,能进一寸进一寸,能进一尺进一尺,不断积累,飞跃必来,突破随之。
目录 Math类(数学、计算相关)
使用方法:Math.方法名()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public static final double E = 2.7182818284590452354 ;public static final double PI = 3.14159265358979323846 ;static Object abs (Object o) static Object min (Object a, Object b) static Object max (Object a, Object b) static double ceil (double a) static double floor (double a) static double pow (double a, double b) static double sqrt (double a) static double toDegrees (double angrad) static double toRadians (double angdeg) static double tan (double a) static double tanh (double x) static double sin (double a) static double sinh (double x) static double cos (double a) static double cosh (double x) static double acos (double a) static double asin (double a) static double atan (double a) static double rint (double a) static long round (double a) static int round (float a) static Object floorDiv (Object x, Object y) static Object floorMod (Object x, Object y) static double hypot (double x, double y)
解释
这里的取模和我们平常的取余有一定的区别。如果两个数符号相同的话,取模和取余结果是一样的,如果两个数字符号不相同的话,那么取模和取余结果那就是天差地别了。注意一点:取余的时候符号和被除数保持一致,取模的时候符合和除数保持一致。注意:Java 中的取余运算是 %,而取模运算是 Math.floorMod()。 具体详细的解释可以自行找API查看。
String类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 int length () boolean isEmpty () boolean isBlank () char charAt (int index) int compareTo (String str) String concat (String str) boolean contains (CharSequence s) boolean equals (Object anObject) boolean startsWith (String prefix) boolean startsWith (String prefix, int toffset) boolean endsWith (String suffix) static String format (String format, Object... args) int indexOf (int ch) int indexOf (int ch, int fromIndex) int lastIndexOf (int ch) int lastIndexOf (int ch, int fromIndex) String replace (char oldChar, char newChar) String replaceFirst (String regex, String replacement) String[] split (String regex) String[] split (String regex, int limit) CharSequence subSequence (int beginIndex, int endIndex) String substring (int beginIndex) String substring (int beginIndex, int endIndex) char [] toCharArray () String toLowerCase () String toUpperCase () String trim () static String valueOf (Object obj) static String valueOf (char [] data, int offset, int count) boolean contentEquals (CharSequence cs) void getChars (int srcBegin, int srcEnd, char [] dst, int dstBegin) int codePointAt (int index) int codePointBefore (int index) boolean matches (String regex)
StringBuilder类 用法也可以用于StringBuffer。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 StringBuilder() StringBuilder(CharSequence seq) StringBuilder(int capacity) StringBuilder(String str) int capacity () int length () StringBuilder append (Object o) char charAt (int index) StringBuilder delete (int start, int end) StringBuilder deleteCharAt (int index) StringBuilder insert (int offset, Object o) StringBuilder insert (int index, char [] str, int offset, int len) StringBuilder insert (int dstOffset, CharSequence s, int start, int end) StringBuilder reverse () void setCharAt (int index, char ch) void setLength (int newLength) String toString () void getChars (int srcBegin, int srcEnd, char [] dst, int dstBegin) int indexOf (String str) int indexOf (String str, int fromIndex) int lastIndexOf (String str) int lastIndexOf (String str, int fromIndex) StringBuilder replace (int start, int end, String str) CharSequence subSequence (int start, int end) String substring (int start) String substring (int start, int end)
Character类
使用方法:Character.方法名()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 Character(char value) static int compare (char x, char y) static int digit (char ch, int radix) static boolean isDigit (char ch) static boolean isDigit (int codePoint) static boolean isLetter (char ch) static boolean isLetter (int codePoint) static boolean isLetterOrDigit (char ch) static boolean isLowerCase (char ch) static boolean isLowerCase (int codePoint) static boolean isUpperCase (char ch) static boolean isUpperCase (int codePoint) static boolean isWhitespace (char ch) static boolean isWhitespace (int codePoint) static boolean isSpaceChar (char ch) static boolean isSpaceChar (int codePoint) String toString () static String toString (char c) static char toLowerCase (char ch) static int toLowerCase (int codePoint) static char toUpperCase (char ch) static int toUpperCase (int codePoint)
Arrays类(数组处理)
使用方法:Arrays.方法名()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 static void sort (Object[] a) static void sort (Object[] a, int fromIndex, int toIndex) static int binarySearch (Object[] a, Object key) static int binarySearch (Object[] a, int fromIndex, int toIndex, Object key) static Object[] copyOf (Object[] original, int newLength) static Object[] copyOfRange (Object[] original, int from, int to) static boolean equals (Object[] a, Object[] a2) static void fill (Object[] a, Object val) static void fill (Object[] a, int fromIndex, int toIndex, Object val) static void parallelPrefix (Object[] array, IntBinaryOperator op) static void parallelPrefix (Object[] array, int fromIndex, int toIndex, IntBinaryOperator op) static void parallelSort (Object[] a) static void parallelSort (Object[] a, int fromIndex, int toIndex) static String toString (Object[] a)
解释
Arrays.parallelPrefix(a, (left, right)-> left + right); 表示将a数组中的元素求一个前缀和。Arrays.parallelPrefix(a, (left, right)-> left * right);表示将a数组中的元素求一个前缀乘积(随便取的名,自行理解)。
Collections类(集合处理)
使用方法:Collections.方法名()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 static <T extends Comparable<? super T>> void sort (List<T> list) static <T> void sort (List<T> list, Comparator<? super T> c) static void swap (List<?> list, int i, int j) static <T> Comparator<T> reverseOrder () static void reverse (List<?> list) static <T> boolean replaceAll (List<T> list, T oldVal, T newVal) static <T> int binarySearch (List<? extends Comparable<? super T>> list, T key) static <T extends Object & Comparable<? super T>> T min (Collection<? extends T> coll) static <T> T min (Collection<? extends T> coll, Comparator<? super T> comp) static <T extends Object & Comparable<? super T>> T max (Collection<? extends T> coll) static <T> T max (Collection<? extends T> coll, Comparator<? super T> comp) static int indexOfSubList (List<?> source, List<?> target) static int lastIndexOfSubList (List<?> source, List<?> target) static int frequency (Collection<?> c, Object o) static <T> void fill (List<? super T> list, T obj) static <T> void copy (List<? super T> dest, List<? extends T> src) static <T> boolean addAll (Collection<T> c, T element1,T element2 ...)
时间类 更多具体详细用法见:蓝桥杯JAVA-8.日期时间处理详解
Date boolean after (Date when) boolean before (Date when) int compareTo (Date anotherDate) boolean equals (Object obj) String toString ()
Date parse (String source) abstract Date parse (String source, ParsePosition pos) void setCalendar (Calendar newCalendar) void setTimeZone (TimeZone zone) TimeZone getTimeZone () String format (Date date)
是DateFormat类的子类。
SimpleDateFormat() SimpleDateFormat(String pattern) SimpleDateFormat(String pattern, DateFormatSymbols formatSymbols) SimpleDateFormat(String pattern, Locale locale)
Calendar类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 boolean after (Object when) boolean before (Object when) void set (int field, int value) void set (int year, int month, int date) void set (int year, int month, int date, int hourOfDay, int minute, int second) void setFirstDayOfWeek (int value) void setMinimalDaysInFirstWeek (int value) void setTime (Date date) void setTimeInMillis (long millis) void setTimeZone (TimeZone value) void setWeekDate (int weekYear, int weekOfYear, int dayOfWeek) Date getTime () long getTimeInMillis () TimeZone getTimeZone () int getWeeksInWeekYear () int getWeekYear () String getCalendarType () String getDisplayName (int field, int style, Locale locale) Map<String,Integer> getDisplayNames (int field, int style, Locale locale) int getFirstDayOfWeek () Instant toInstant () String toString () int get (int field)
日期时间常用操作
指定格式输出
String tm = "2021年7月13日" ; SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日" ); Date date = sdf.parse(tm); SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd" ); System.out.printf(sdf2.format(date));2021 /07 /13
时间位置
Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒SSS毫秒" ); System.out.println(sdf.format(date)); SimpleDateFormat sdf2 = new SimpleDateFormat("E 一年中第w周的第D天 一个月中的第W周的周F 一天中的24制的第k小时(点)12制的第K小时(点)" ); System.out.println(sdf2.format(date));2021 年12 月14 日 14 时53 分59 秒997 毫秒 星期二 一年中第51 周的第348 天 一个月中的第3 周的周2 一天中的24 制的第14 小时(点)12 制的第2 小时(点)
日期加减
public static void main (String[] args) { Calendar c1 = Calendar.getInstance(); c1.set(2021 , 1 , 1 ); System.out.println(c1.get(Calendar.YEAR) +"-" +c1.get(Calendar.MONTH) +"-" +c1.get(Calendar.DATE)); c1.set(2021 , 1 , 0 ); System.out.println(c1.get(Calendar.YEAR) +"-" +c1.get(Calendar.MONTH) +"-" +c1.get(Calendar.DATE)); }2021 -1 -1 2021 -0 -31
具体原理和具体使用方法见,蓝桥杯JAVA-8.日期时间处理详解
BigInteger类(大整数) 更多具体详细用法见:蓝桥杯JAVA-6.大数(整数、小数)处理
BigDecimal类(大浮点数) 更多具体详细用法见:蓝桥杯JAVA-6.大数(整数、小数)处理