Java基础之日期时间

java.util 包提供了 Date 类来封装当前的日期和时间。

项目中常用的时间:

TimeUtil工具类

package com.shanyue.syonehealth.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class TimeUtil {

    //获取当前系统时间
    public static String getSystemTime(String sfm) {
        if (sfm == null) sfm = "yyyy-MM-dd";
        Date date = new Date();
        SimpleDateFormat forager = new SimpleDateFormat(sfm);
        return forager.format(date);
    }

    //获取当前周几
    public static String getWeekOfDate(Date date) {
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    //获取当日几个月后前后时间
    public  static String getMonthOfDate(int m,String sfm) {
        if (sfm == null) sfm = "yyyy-MM-dd";
        Calendar date = Calendar.getInstance();
        date.setTime(new Date());
        date.add(Calendar.MONTH,m);
        SimpleDateFormat formatter = new SimpleDateFormat(sfm);
        return formatter.format(date.getTime());
    }

    //获取当日几天前后时间
    public  static String getAfterDay(int m,String sfm) {
        if (sfm == null) sfm = "yyyy-MM-dd";
        Calendar date = Calendar.getInstance();
        date.setTime(new Date());
        date.add(Calendar.DATE,m);
        SimpleDateFormat formatter = new SimpleDateFormat(sfm);
        return formatter.format(date.getTime());

    }

    // 获取当月1号
    public static  String  getNowMonth(String fmt) {
        if(fmt==null)fmt="yyyy-MM-dd";
        Calendar date = Calendar.getInstance();
        date.set(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat formatter = new SimpleDateFormat(fmt);
        return formatter.format(date.getTime());
    }
    //获取当月最后一天
    public static  String  getFinallyMonth(String fmt) {
        int maxCurrentMonthDay;
        if(fmt==null)fmt="yyyy-MM-dd";
        Calendar date

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java之眼

创作不易,一起努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值