Home >Java >javaTutorial >Share commonly used APIs in JAVA
java.util.Date Date class
Constructor method
public Date() returns the current time, which is the time expressed in milliseconds from 1970-1-1
public Date(long millisecond value) Returns the time of the specified millisecond value
Method
long getTime() returns the millisecond value of the current time
void setTime(long millisecond value) Sets the millisecond of the current time Value
Time origin: 1970-01-01 00:00:00
Milliseconds: thousandths of a second 1 second = 1000 milliseconds
java.text.DateFormat Date Formatting class
The abstract class cannot create objects and needs to use its subclass java.text.SimpleDateFormat
Constructor method
public SimpleDateFormat() Default format creates date formatting class object
public SimpleDateFormat( String pattern) Given format
Convert the date to a string in the specified format
String format(Date date) Convert the date to a string
Convert the string to a date
Date parse(String source ) Convert string to date
1 public static void main(String[] args) throws ParseException { 2 //将当前时间转换为指定格式的字符串 3 //创建当前时间对象Date 4 Date now = new Date(); 5 //创建日期格式化类对象DateFormat 6 DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss E"); 7 //调用format方法 将日期转换为字符串 8 String s = df.format(now); 9 //打印字符串10 System.out.println(s);11 System.out.println("---------------------");12 //获取1949-10-01这个字符串时间的毫秒值13 //定义字符串14 String sDate = "1949-10-01";15 //创建日期格式化类对象DateFormat对象16 DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");17 //调用parse方法 将字符串转换为Date类型对象18 Date d = df2.parse(sDate);19 //调用Date类的getTime方法获取毫秒值20 long time = d.getTime();21 System.out.println(time);22 }
java.util.Calendar Calendar class
Static method creates object
Calendar getInstance() returns Calendar subclass object
Method
int get(int field) Get the time based on the given field
void set(int field,int time) Set the time of the specified field
void set(int year, int month, int day) Directly set the year, month and day
Date getTime() Convert calendar object to date object
void add(int field, int time) Add or subtract time for the specified customization
1 public static void main(String[] args) { 2 Calendar c = Calendar.getInstance(); 3 System.out.println(c); 4 5 int i = c.get(Calendar.MONTH); 6 System.out.println(i); 7 i = c.get(Calendar.DATE); 8 System.out.println(i); 9 i = c.get(Calendar.DAY_OF_MONTH);10 System.out.println(i);11 System.out.println("---------------------");12 //将年份设置为 201813 c.set(Calendar.YEAR, 2018);14 //将月份设置为10月15 c.set(Calendar.MONTH, 9);16 //直接设置年月日17 c.set(1949, 9, 1);18 System.out.println("-------------");19 //为指定年份 -120 c.add(Calendar.YEAR, -1);21 22 //将日期转换为日期23 Date d =c.getTime();24 System.out.println(d);25 }
1 获取出生了多少天案例代码 2 public static void main(String[] args) throws Exception { 3 Scanner sc = new Scanner(System.in); 4 System.out.println("请输入您的出生日期(xxxx-xx-xx):"); 5 String birthDay = sc.nextLine(); 6 int day = getDay(birthDay); 7 System.out.println("您出生了:"+day+"天"); 8 } 9 /*10 * 将给定的字符串转换为Date类 获取毫秒值11 * 创建时间对象 获取毫秒值12 * 天数 = (当前时间毫秒值 - 生日时间毫秒值)/1000/60/60/2413 */14 public static int getDay(String brithDay) throws Exception{15 //创建DateFormat对象16 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");17 //调用parse方法 将字符串转换为Date类型18 Date brith = df.parse(brithDay);19 //获取生日日期的毫秒值20 long birthTime = brith.getTime();21 //获取当前时间的毫秒值22 long nowTime = new Date().getTime();23 //天数 = (当前时间毫秒值 - 生日时间毫秒值)/1000/60/60/2424 int day = (int)((nowTime-birthTime)/1000/60/60/24);25 return day;26 }
1 获取指定年份的2月有多少天 2 思路: 3 获取到指定年份的3月1号 4 将日期天数-1 即使2月的最后一天 5 获取对象的当前月的天数 6 public static void main(String[] args) { 7 int day = getDay(2016); 8 System.out.println(day); 9 }10 public static int getDay(int year){11 //创建Calendar对象12 Calendar c = Calendar.getInstance();13 //获取到指定年份的3月1号14 c.set(year, 2, 1);15 //将日期-116 c.add(Calendar.DATE, -1);17 //获取当前月的日期18 int day = c.get(Calendar.DAY_OF_MONTH);19 20 // Date time = c.getTime();21 // System.out.println(time);22 23 return day;24 }25
Array tool class java.util.Arrays
static void sort(xxx[] xx) array Sorting
static String toString(xxx[] xx) Convert array to string
java.lang.System
Constructor method private cannot create objects All methods static class name Call directly
Method
static void exit(int status code) Exit the virtual machine 0 Normal exit
static void gc() implies that the garbage collector collects garbage
static String getProperty(String key) Get the system's Specify the attribute value
Get the current time millisecond value
1.Date class getTime method
2.Calendar class long getTimeInMillis()
3.System class static long currentTimeMillis ()
Copy of array
static void arraycopy(Object src,
int srcPos,
Object dest,
int destPos,
int length )
Copy array
object src: source array
int srcPos: index of source array to start copying
Object dest: target array
int destPos: index of target array copied to
int length: length of copy
Mathematical tool class
java.lang.Math
static int abs(int a) Return absolute value
static long round(double a) Rounding
static int round(float a)
static double ceil(double a) Rounding up
static double floor(double a) Rounding down
static int max(int a, int b) Maximum value of two numbers
static int min(int a, int b) Minimum value of two numbers
static double pow(double a, double b) b power of a
java.lang.Integer
Convert string to basic type
static int parseInt(String s)
Convert basic type to string
String s = 1+"";
String Method String valueOf(int a)
Conversion between basic types and objects
int---Integer
public Integer(int i)
public Integer( String i)
Integer---int
int intValue()
JDK1.5 Automatic unboxing and automatic packing
int -- Integer
Integeer --int
byte constant pool details, if the defined number is between -128 and 127 and this variable exists, the JVM will no longer create new objects
Regular expression
[] Choose any one of the contents inside
[^ ] Except the contents inside
[a-zA-Z_0-9] Alphanumeric and underline can be put together. Got a name word character
? 0-1 times
* Any number of times
+ At least 1 time
{n} Exactly n times
{n,} At least n times
{ n,m} n-m times includes n including m
1 正则表达式:表示正确规则的字符串 2 检验QQ号 3 1.长度 5-12 4 2.0不能开头 5 3.纯数字 6 public static boolean checkQQ2(String qq) { 7 // String regex = "[1-9][0-9]{4,11}"; 8 // boolean b = qq.matches(regex); 9 // return b;10 11 return qq.matches("[1-9][0-9]{4,11}");12 }
The above is the detailed content of Share commonly used APIs in JAVA. For more information, please follow other related articles on the PHP Chinese website!