格式化输出:实现分时问候/当前年月日周时分秒/当前时间周x

作业三:

  • 编写程序实现分时问候

public class date{
    public static void main(String[] args) {
        Calendar d = Calendar.getInstance();
        int hour = d.get(Calendar.HOUR_OF_DAY);
        if (hour >= 6 && hour < 8) {
            System.out.print("早上好,小甜心");
        } else if (hour >= 8 && hour < 12) {
            System.out.print("上午好,小甜心");
        } else if (hour >=12 && hour < 14) {
            System.out.print("中午好,小甜心");
        }else if (hour >=14 && hour < 18) {
            System.out.print("中午好,小甜心");
        }else if (hour >=18 && hour < 22) {
            System.out.print("中午好,小甜心");
        }else if (hour >=22) {
            System.out.print("深夜了,小甜心");
        }
    }
}
  •  

  • 现在时间是:x年x月x日 周x x时x时x分x秒

import java.text.SimpleDateFormat;
import java.util.Date;
public class date
{
    public static void main(String[] args)
    {
        Date now=new Date();    //创建一个Date对象,获取当前时间
        //指定格式化格式
        SimpleDateFormat f=new SimpleDateFormat("现在时间是 "+"yyyy 年 MM 月 dd 日 E HH 点 mm 分 ss 秒");
        System.out.println(f.format(now));    //将当前时间袼式化为指定的格式
    }
}

  • 编写程序输入今日是星期几

public class date {
    public static void main(String[] args) {
        System.out.printf(" %1$tA",System.currentTimeMillis());
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值