DateUtil.class(日期时间转换的工具类)

本文介绍了一套用于处理日期和时间转换的实用工具类,包括获取当前时间、格式化日期/时间、仅获取日期或时间等功能,适用于Android平台。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import android.annotation.SuppressLint;
import android.text.format.Time;

import java.text.SimpleDateFormat;
import java.util.Date;

/**

  • 时间、日期转换工具

  • Created by mythmayor on 2016/9/14.
    */
    public class DateUtils {

    /**

    • 获取当前时间
    • @return 年月日时分秒字符串
      */
      public static String getCurrentTime() {
      Time time = new Time(“GMT+8”);
      time.setToNow();
      String year = “” + time.year;
      int imonth = time.month + 1;
      String month = imonth > 9 ? “” + imonth : “0” + imonth;
      String day = time.monthDay > 9 ? “” + time.monthDay : “0”
      + time.monthDay;
      String hour = (time.hour + 8) > 9 ? “” + (time.hour + 8) : “0”
      + (time.hour + 8);
      String minute = time.minute > 9 ? “” + time.minute : “0” + time.minute;
      String sec = time.second > 9 ? “” + time.second : “0” + time.second;
      String currentTime = year + month + day + hour + minute + sec;
      return currentTime;
      }

    //获取 日期/时/分/秒
    @SuppressLint(“SimpleDateFormat”)
    public static String getDateTime(long time) {
    SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
    String date = sdf.format(new Date(time));
    return date;
    }

    //获取 日期/时/分
    @SuppressLint(“SimpleDateFormat”)
    public static String getHourMinute(long time) {
    SimpleDateFormat sdf = new SimpleDateFormat(“yyyy年MM月dd日HH时mm分”);
    String date = sdf.format(new Date(time));
    return date;
    }

    //获取 日期年月日
    @SuppressLint(“SimpleDateFormat”)
    public static String getYearMonthDay(long time) {
    SimpleDateFormat sdf = new SimpleDateFormat(“yyyy年MM月dd日”);
    String date = sdf.format(new Date(time));
    return date;
    }

    //获取日期年月
    public static String getYearMonth(long time){
    SimpleDateFormat sdf = new SimpleDateFormat(“yyyy年MM月”);
    String date = sdf.format(new Date(time));
    return date;
    }

    //获取日期年月
    public static String getYearMonth2(long time){
    SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM”);
    String date = sdf.format(new Date(time));
    return date;
    }

    //获取日期年
    public static String getYear(long time){
    SimpleDateFormat sdf = new SimpleDateFormat(“yyyy年”);
    String date = sdf.format(new Date(time));
    return date;
    }

    //获取 时/分
    public static String getTime(long time) {
    SimpleDateFormat sdf = new SimpleDateFormat(“HH:mm”);
    String date = sdf.format(new Date(time));
    return date;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值