Time

package com.share.tool.oman.date;

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

public class SystemDateClass {
 public static String getDateNow() {
  String now = "";
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  now = sdf.format(new Date());
  return now;
 }

 public static String getYYYYMMddHHmmss() {
  String now = "";
  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
  now = sdf.format(new Date());
  return now;
 }

 public static String getDate() {
  String now = "";
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  now = sdf.format(new Date());
  return now;
 }

 public static String getYear() {
  String now = "";
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
  now = sdf.format(new Date());
  return now;
 }

 public static String getMonth() {
  String now = "";
  SimpleDateFormat sdf = new SimpleDateFormat("MM");
  now = sdf.format(new Date());
  return now;
 }

 public static String getPrvMonth() {
  Calendar tt = Calendar.getInstance();
  String now = getDate();
  String[] tmp = now.split("-");
  tt.set(new Integer(tmp[0]), new Integer(tmp[1]) - 2,
    new Integer(tmp[2]));
  SimpleDateFormat sdf = new SimpleDateFormat("MM");
  now = sdf.format(tt.getTime());
  return now;
 }

 public static String getDay() {
  String now = "";
  SimpleDateFormat sdf = new SimpleDateFormat("DD");
  now = sdf.format(new Date());
  return now;
 }

 public static int getDaysByMonth(int year, int month) {
  int m = 0;
  switch (month) {
  case 1:
   m = 31;
   break;
  case 2:
   if (year % 4 == 0 && year % 100 != 0)
    m = 29;
   else
    m = 28;
   break;
  case 3:
   m = 31;
   break;
  case 4:
   m = 30;
   break;
  case 5:
   m = 31;
   break;
  case 6:
   m = 30;
   break;
  case 7:
   m = 31;
   break;
  case 8:
   m = 31;
   break;
  case 9:
   m = 30;
   break;
  case 10:
   m = 31;
   break;
  case 11:
   m = 30;
   break;
  case 12:
   m = 31;
   break;
  }
  return m;
 }

 /***
  *
  * @param date
  *            日期字符串格式(yyyy-MM-dd)
  * @return 返回日期类
  */
 public static Date getDateByString(String date) {
  Calendar tt = Calendar.getInstance();
  String[] tmp = date.split("-");
  tt.set(new Integer(tmp[0]), new Integer(tmp[1]) - 1,
    new Integer(tmp[2]));

  return tt.getTime();
 }

 /***
  *
  * @param date
  *            日期字符串格式(yyyy-MM-dd HH:mm:ss)
  * @return 返回日期类
  */
 public static Date getDateByNowString(String dateNow) {
  Calendar tt = Calendar.getInstance();
  String[] tmp1 = dateNow.split(" ");
  String[] tmp = tmp1[0].split("-");
  String[] tmp2 = tmp1[1].split(":");
  tt.set(new Integer(tmp[0]), new Integer(tmp[1]) - 1,
    new Integer(tmp[2]), new Integer(tmp2[0]),
    new Integer(tmp2[1]), new Integer(tmp2[2]));

  return tt.getTime();
 }

 /***
  *
  * @param date
  *            要转换的日期类
  * @param format
  *            日期返回格式
  * @return 返回日期字符串
  */
 public static String getDateStringByDateAndFormat(Date date, String format) {
  SimpleDateFormat sdf = new SimpleDateFormat(format);
  return sdf.format(date);
 }

 public static String getDateStringByAddDay(Integer day) {
  String date = "";
  Date d = new Date();
  long l = ((long) day) * 24 * 60 * 60 * 1000 + d.getTime();
  d.setTime(l);
  date = getDateStringByDateAndFormat(d, "yyyy-MM-dd HH:mm:ss");
  return date;
 }

 public static String getDateStringByjianDay(Integer day) {
  String date = "";
  Date d = new Date();
  long l = d.getTime() - ((long) day) * 24 * 60 * 60 * 1000;
  d.setTime(l);
  date = getDateStringByDateAndFormat(d, "yyyy-MM-dd HH:mm:ss");
  return date;
 }
 
 public  static String sysdatebyString(String date){
  Date d = new Date(new Long(date));
  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  return format.format(d);
 }

}

### Python `time.time()` 函数使用说明 `time.time()` 是 Python 的标准库模块 `time` 中的一个函数,用于返回当前时间的时间戳(即自 Unix 纪元以来的秒数)。Unix 纪元通常定义为 1970 年 1 月 1 日 00:00:00 UTC。 #### 函数签名 ```python time.time() ``` 该函数不需要参数,并返回一个浮点数值表示的时间戳[^2]。 #### 示例代码 以下是一个简单的例子展示如何使用 `time.time()` 来获取当前时间的时间戳: ```python import time current_time = time.time() print(f"Current Time (Timestamp): {current_time}") ``` 如果希望将这个时间戳转换成可读的日期时间字符串,则可以结合 `time.localtime()` 或者 `time.gmtime()` 方法来实现。例如: ```python import time timestamp = time.time() # 获取当前时间戳 local_time = time.localtime(timestamp) # 转换成本地时间结构体 formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", local_time) print(f"Formatted Local Time: {formatted_time}") ``` 上述代码中的 `%Y`, `%m`, `%d`, `%H`, `%M`, 和 `%S` 都是格式化字符,分别代表年份、月份、日、小时、分钟和秒钟。 另外需要注意的是,在某些情况下可能遇到更高精度的需求,比如毫秒级或者微秒级的时间记录。此时可以通过乘以适当的比例因子得到更精确的结果: ```python milliseconds_since_epoch = int(time.time() * 1000) microseconds_since_epoch = int(time.time() * 1e6) print(f"Milliseconds Since Epoch: {milliseconds_since_epoch}") print(f"Microseconds Since Epoch: {microseconds_since_epoch}") ``` 以上展示了如何利用 `time.time()` 结合其他操作获得不同粒度上的时间数据。 #### 注意事项 当处理跨平台应用时需注意,默认情况下 `time.time()` 提供的是基于系统的UTC时间;然而实际显示可能会受到本地环境设置的影响,因此建议在必要场合下显式指定所需时区信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值