Java日期和时间学习

2023-12-22 16:32:48

获取当月的第一天和最后一天

import java.util.Calendar;

public class Main {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);

        // 获取当月第一天
        calendar.set(year, month, 1);
        String firstDayOfMonth = String.format("%04d-%02d-%02d", year, month + 1, 1);

        // 获取当月最后一天
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        String lastDayOfMonth = String.format("%04d-%02d-%02d", year, month + 1, calendar.get(Calendar.DAY_OF_MONTH));

        System.out.println("当月第一天:" + firstDayOfMonth);
        System.out.println("当月最后一天:" + lastDayOfMonth);
    }
}

获取当前周的第一天和最后一天

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

public class Main {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();

        // 获取当前周第一天
        LocalDate firstDayOfWeek = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        String strFirstDayOfWeek = firstDayOfWeek.toString();

        // 获取当前周最后一天
        LocalDate lastDayOfWeek = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
        String strLastDayOfWeek = lastDayOfWeek.toString();

        System.out.println("当前周第一天:" + strFirstDayOfWeek);
        System.out.println("当前周最后一天:" + strLastDayOfWeek);
    }
}

获取上个月的第一天和最后一天

import java.util.Calendar;

public class Main {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1); // 上个月

        // 获取上个月第一天
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        String firstDayOfLastMonth = String.format("%04d-%02d-%02d",
                calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH));

        // 获取上个月最后一天
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        String lastDayOfLastMonth = String.format("%04d-%02d-%02d",
                calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH));

        System.out.println("上个月第一天:" + firstDayOfLastMonth);
        System.out.println("上个月最后一天:" + lastDayOfLastMonth);
    }
}

获取上个周的第一天和最后一天

import java.util.Calendar;

public class Main {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.WEEK_OF_YEAR, -1); // 上个周

        // 获取上个周第一天(假设周一为一周的开始)
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String firstDayOfLastWeek = String.format("%04d-%02d-%02d",
                calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH));

        // 获取上个周最后一天(假设周日为一周的结束)
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        String lastDayOfLastWeek = String.format("%04d-%02d-%02d",
                calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH));

        System.out.println("上个周第一天:" + firstDayOfLastWeek);
        System.out.println("上个周最后一天:" + lastDayOfLastWeek);
    }
}

获取昨天

import java.time.LocalDate;

public class Main {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        LocalDate yesterday = today.minusDays(1);

        String strYesterday = yesterday.toString();
        System.out.println("昨天日期:" + strYesterday);
    }
}

格式化秒为X天X时X分X秒

public class MainTime {

    /**
     * 格式化秒为X天X时X分X秒
     *
     * @param second
     * @return
     */
    public static String secondsTo(Long second) {
        if (second == null) {
            return "";
        }
        long y = second / 86400;
        long h = (second / 3600) % 24;
        long m = (second / 60) % 60;
        long s = second % 60;
        String res = "";
        if (y != 0) {
            res += y + "天";
        }
        if (h != 0) {
            res += h + "时";
        }
        if (m != 0) {
            res += m + "分";
        }
        if (s != 0) {
            res += s + "秒";
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(secondsTo(134343L));
    }
}

文章来源:https://blog.csdn.net/NN2016160626/article/details/135154536
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。