目录
1.Math类
1.绝对值
System.out.println(Math.abs(-156));
2.向上取整
System.out.println(Math.ceil(2.14));//3
3.向下取整
System.out.println(Math.floor(3.99));//3
4.四舍五入
System.out.println(Math.round(3.62));
System.out.println(Math.round(3.49));
5.最大值
System.out.println(Math.max(3,9));
6.最小值
System.out.println(Math.min(8,9));
7.求次幂
System.out.println(Math.pow(2,4));
8.返回0~1间的随机数
System.out.println(Math.random());
9.1~4的随机数
System.out.println((int)(Math.random()*40000000)%4+1);
2.System类
时间戳
public class Demo {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
method();
long endTime = System.currentTimeMillis();
System.out.println("输出100次的时间"+(endTime-startTime)+"ms");
}
private static void method(){
for (int i = 0; i < 100; i++) {
i += 1;
System.out.print(i+" ");
}
}
}
这段代码是一个Java程序,主要用来测试一个方法的执行时间。
在main
方法中,首先记录了程序开始执行的时间startTime
(以毫秒为单位)。然后调用了method
方法,该方法用来输出从1到100的数字。method
方法中使用了一个循环,对变量i
进行从0到99的迭代,每次循环变量i
自增1,并输出i
的值。这样就会输出从1到100的数字。
method
方法执行完后,再记录当前时间endTime
。最后输出了程序执行一百次的时间(以毫秒为单位),即endTime-startTime
的值。
整个程序的目的是为了测试method
方法的执行时间。通过记录开始时间和结束时间,可以计算出方法的执行时间。
3.Date类
1.获取系统当前时间
Date date = new Date();//获取当前系统时间
System.out.println(date);
2.获取前一天时间
Date date = new Date(System.currentTimeMillis()-1000*60*60*24);//前一天的时间
System.out.println(date);
3.获取后一天时间
Date date = new Date(System.currentTimeMillis()+1000*60*60*24);//后一天的时间
System.out.println(date);
4.SimpleDateFormat
使用SimplDateFormat 利用 Date对象格式化输出指定时间
public class Test {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = sdf.format(new Date());
System.out.println(s);
String time = "2024-3-5 15:19:28";
SimpleDateFormat newsdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = newsdf.parse(time);
System.out.println(parse);
}
}
首先,在main
方法中创建了一个SimpleDateFormat
对象sdf
,用来指定日期时间的格式为"yyyy-MM-dd HH:mm:ss"。然后,使用sdf
格式化当前的日期时间,并将结果赋值给字符串s
。然后通过System.out.println
语句输出了s
的值,即当前的日期时间。
接下来,定义了一个字符串变量time
,用于指定一个特定的日期时间,格式为"yyyy-MM-dd HH:mm:ss"。然后创建了另一个SimpleDateFormat
对象newsdf
,也是使用相同的日期时间格式。
然后,使用newsdf
的parse
方法将字符串time
解析为一个Date
对象,并将结果赋值给变量parse
。
最后,通过System.out.println
语句输出了parse
的值,即解析后的日期时间。
整个程序的目的是为了演示如何使用SimpleDateFormat
类来格式化和解析日期时间。首先,将当前的日期时间格式化为指定的格式;然后,将指定的日期时间字符串解析为Date
对象。
函数方式实现
public class Demo02 {
/**
* 将Date类型转换成指定格式的字符串
* @param date 要转换的对象
* @param format 转换的目标格式
* @return 转换之后的字符串
*/
private static String datetoString(Date date,String format){
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
/**
* 将时间类型的字符串根据指定的格式转换成Date对象
* @param str 用来表示时间的字符串
* @param format 字符串所对应的时间格式
* @return 转换之后的Date对象
*/
private static Date Stringtodate(String str, String format) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.parse(str);
}
public static void main(String[] args) throws ParseException {
System.out.println(datetoString(new Date(),"yyyy-MM-dd HH:mm:ss"));
System.out.println(Stringtodate("2024-2-10 11:23:56","yyyy-MM-dd HH:mm:ss"));
}
}
首先,在程序中定义了两个私有静态方法,分别是datetoString
和Stringtodate
。
datetoString
方法用于将Date
类型的日期时间对象转换成指定格式的字符串。在方法中,首先创建了一个SimpleDateFormat
对象sdf
,并将格式参数format
传入。然后,使用sdf
的format
方法将date
对象格式化为指定格式的字符串,并将结果返回。
Stringtodate
方法则是将时间类型的字符串根据指定的格式转换成Date
对象。在方法中,同样创建了一个SimpleDateFormat
对象sdf
,并将格式参数format
传入。然后,使用sdf
的parse
方法将字符串str
解析为Date
对象,并将结果返回。
在main
方法中,通过调用datetoString
方法将当前的日期时间转换为格式为"yyyy-MM-dd HH:mm:ss"的字符串,并使用System.out.println
语句输出结果。
接着,调用Stringtodate
方法将字符串"2024-2-10 11:23:56"按照格式"yyyy-MM-dd HH:mm:ss"解析为Date
对象,并使用System.out.println
语句输出结果。
整个程序的目的是为了演示如何使用SimpleDateFormat
类进行日期时间的格式转换。其中,datetoString
方法用于将Date
对象转换为字符串,Stringtodate
方法用于将字符串转换为Date
对象。
5.Calender类
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH)+1; //MONTH从0开始
int day = cal.get(Calendar.DATE);
System.out.printf("%d-%d-%d\n",year,month,day);
cal.add(Calendar.YEAR,1);
cal.add(Calendar.MONTH,1);
cal.add(Calendar.DATE,5);
year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH)+1;
day = cal.get(Calendar.DATE);
System.out.printf("%d-%d-%d\n",year,month,day);
//重置时间
cal.setTime(new Date());
year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH)+1;
day = cal.get(Calendar.DATE);
System.out.printf("%d-%d-%d\n",year,month,day);
首先,通过Calendar.getInstance()
方法获取一个Calendar
对象cal
,默认是当前日期时间。
然后,通过cal.get(Calendar.YEAR)
方法获取当前日期时间的年份,赋值给变量year
;通过cal.get(Calendar.MONTH)
方法获取当前日期时间的月份,由于Calendar.MONTH
从0开始,所以需要加1,最后赋值给变量month
;通过cal.get(Calendar.DATE)
方法获取当前日期时间的日期,赋值给变量day
。然后,使用System.out.printf
方法按照格式"%d-%d-%d\n"输出年、月、日。
接着,通过cal.add(Calendar.YEAR,1)
方法将年份加1,通过cal.add(Calendar.MONTH,1)
方法将月份加1,通过cal.add(Calendar.DATE,5)
方法将日期加5。再次使用上述方式获取新的年、月、日,并输出。
最后,通过cal.setTime(new Date())
方法将cal
对象重置为当前日期时间。再次使用上述方式获取新的年、月、日,并输出。
整个程序的目的是为了演示如何使用Calendar
类来获取和修改日期时间的年、月、日,以及如何重置Calendar
对象。通过Calendar
类的相关方法,可以实现对日期时间的灵活操作。