package day13;
import java.text.SimpleDateFormat;
import java.util.Date;
//2.請(qǐng)用代碼實(shí)現(xiàn):獲取當(dāng)前的日期,并把這個(gè)日期轉(zhuǎn)換為指定格式的字符串,如2088-08-08
public class TestDate6
{
public static void main(String[] args)
{
Date date = new Date();
System.out.println(date);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String str=simpleDateFormat.format(date);
System.out.println(str);
}
}
2.png
3.計(jì)算你出生有多少天了遗契?
package day13;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Test
{
public static void main(String[] args) throws ParseException
{
function();
}
public static void function() throws ParseException
{
System.out.println("請(qǐng)輸入您旁邊伙伴的出生日期 格式 yyyy-MM-dd");
String birthdayString = new Scanner(System.in).next();
Date currentDate = new Date();
// 將birthdayString轉(zhuǎn)化成 Date對(duì)象
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date birthdayDate = df.parse(birthdayString);
// 1漾根、 獲取當(dāng)前時(shí)間對(duì)應(yīng)的毫秒值
long currentDateTime = currentDate.getTime();
// 2辐怕、 獲取出生時(shí)間對(duì)應(yīng)的毫秒值
long birthdayDateTime = birthdayDate.getTime();
// 3 、 相減
long resultDateTime = currentDateTime - birthdayDateTime;
if (resultDateTime < 0) {
System.out.println("老鐵逗我呢是牢,你還沒出生呢");
} else {
System.out.println(resultDateTime / 1000 / 60 / 60 / 24);
}
}
}