java获取阳历、星期以及当前时间

一、获取阳历:

import java.text.SimpleDateFormat;

import java.util.Date;

public class GetTime{

static String getYangLi(){
    Date d=new Date();//获取时间
    SimpleDateFormat sdf=new SimpleDateFormat("YYYY年MM月dd日");//格式
//如果格式"YYYY年MM月dd日"则月日如果是个位,则前面进行补零(如:02月)
//如果格式"YYYY年M月d日"则月日不进行格式(如:2月)
    String time=sdf.format(d);//对时间进行格式化
    return time;
}
public static void main(String[] args) {
    System.out.println(getYangLi());
}

}

结果:

2022年03月05日

---------------------------------------------------------------------------------------------------------------------------------

二、获取星期

import java.text.SimpleDateFormat;

import java.util.Date;

public class GetTime{

static String getXingQi(){
    Date d=new Date();//获取时间
    SimpleDateFormat sdf=new SimpleDateFormat("EEEE");//格式
    String time=sdf.format(d);//对时间进行格式化
    return time;
}
public static void main(String[] args) {
    System.out.println(getYangLi());
}

}

结果:

星期六

---------------------------------------------------------------------------------------------------------------------------------

三、获取当前时间

import java.text.SimpleDateFormat;

import java.util.Date;

public class GetTime{

static String getShiJian(){
    Date d=new Date();//获取时间
    SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");//格式
//如果格式"HH:mm:ss"则时分秒如果是个位,则前面进行补零(如:02月)
//如果格式"YYYY年M月d日"则时分秒不进行格式(如:10:2:4)
    String time=sdf.format(d);//对时间进行格式化
    return time;
}
public static void main(String[] args) {
    System.out.println(getYangLi());
}

}

结果:

16:14:08

阴历时间我发现有人比我写的好,就不在这里献丑了:获取阴历

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值