java获取当前时间/时间戳

 

JAVA:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 5); //前5小时
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("五个小时前的时间:" + df.format(calendar.getTime()));   //日期类型当前时间前5小时
System.out.println("当前的时间:" + df.format(new Date()));  //日期类型当前时间


Long time1 = System.currentTimeMillis();  //获取当前时间戳(精确到毫秒)
long time = (time1+1800000)/1000;     //获取当前时间后半小时的时间戳(精确到毫秒)
System.out.println("当前的时间:" + time);

Python:

import time
from datetime import date, timedelta, datetime
import datetime as datetime1

yesterday = (date.today() + timedelta(days=-1)).strftime("%Y-%m-%d %H:%M:%S")  # 昨天日期0点
nowtoday = date.today().strftime("%Y-%m-%d %H:%M:%S")  # 今天日期0点
oldServenDay = (date.today() + timedelta(days=-7)).strftime("%Y-%m-%d %H:%M:%S")  # 一周前日期0点

nowTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))  # 当前日期年月日格式
nowTimeMs = int(time.time())  # 当前日期秒值

now = datetime1.datetime.now()
this_month_starttime = datetime1.datetime(now.year, now.month, 1)  # 本月月月初
this_month_start = this_month_starttime.strftime("%Y-%m-%d %H:%M:%S")  # 本月月月初转换为SqlAlchemy可识别的日期格式
this_month_endtime = datetime1.datetime(now.year, now.month + 1, 1) - timedelta(days=1)  # 每月月末
this_month_end = this_month_endtime.strftime("%Y-%m-%d %H:%M:%S")
last_month_endtime = (this_month_starttime - timedelta(days=1))  # 上月月末
last_month_end = last_month_endtime.strftime("%Y-%m-%d %H:%M:%S")  # 上月月末转换为SqlAlchemy可识别的日期格式
last_month_starttime = datetime1.datetime(last_month_endtime.year, last_month_endtime.month, 1)  # 上月月初
last_month_start = last_month_starttime.strftime("%Y-%m-%d %H:%M:%S")  # 上月月初转换为SqlAlchemy可识别的日期格式
# 获取几天前的数据 olday为负数,前一天为负一
def getdate_oldday(oldday):
    return (date.today() + timedelta(days=oldday)).strftime("%Y-%m-%d %H:%M:%S")


# 时间戳转日期, 返回日期格式
def timrstamp_cast_date(timeMs):
    return time.strftime('%Y-%m-%d', time.localtime(timeMs))


# 时间戳转日期, 返回日期格式
def timrstamp_cast_date_hms(timeMs):
    return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timeMs))


# 日期转换为时间戳  返回秒值
def time_cast_timestmap(date):
    timeArray = time.strptime(date, "%Y-%m-%d %H:%M:%S")
    timeStamp = int(time.mktime(timeArray))
    return timeStamp
# 获取今天凌晨0点 年月日时分秒格式
def get_zerotime_nowtoday():
    now = datetime.now()
    # 获取今天零点
    zeroToday = (now - timedelta(hours=now.hour, minutes=now.minute, seconds=now.second,
                                 microseconds=now.microsecond)).strftime("%Y-%m-%d %H:%M:%S")
    return zeroToday


# 获取昨天凌晨0点 年月日时分秒格式
def get_zerotime_yesterday():
    now = datetime.now()
    # 获取今天零点
    zeroyesterday = (now - timedelta(days=1, hours=now.hour, minutes=now.minute, seconds=now.second,
                                     microseconds=now.microsecond)).strftime("%Y-%m-%d %H:%M:%S")
    return zeroyesterday


# 获取当前时间 年月日时分秒格式
def get_nowtime():
    return datetime.now().strftime('%Y-%m-%d %H:%M:%S')


# 前几天的时间的凌晨,beforDay,7天钱就是7 qi
def get_time_beforday(beForDay):
    return (date.today() + timedelta(days=-beForDay)).strftime("%Y-%m-%d %H:%M:%S")


# 获取当前月第一天
def get_month_firstday():
    now_time = datetime.now()
    return datetime(now_time.year, now_time.month, 1, 00, 00, 00)
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值