time()的用法

摘自MSDN

Gets the system time. 需要包含头文件 “time.h”

原型:time_t time( time_t *timer );

Remarks

The time function returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time, according to the system clock. The return value is stored in the location given by timer. This parameter may be NULL, in which case the return value is not stored.


用法示例:

/* TIMES.C illustrates various time and date functions including:
 *      time            _ftime          ctime       asctime
 *      localtime       gmtime          mktime      _tzset
 *      _strtime        _strdate        strftime
 *
 * Also the global variable:
 *      _tzname
 */

#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>

void main()
{
    char tmpbuf[128], ampm[] = "AM";
    time_t ltime;
    struct _timeb tstruct;
    struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };

    /* Set time zone from TZ environment variable. If TZ is not set,
     * the operating system is queried to obtain the default value 
     * for the variable. 
     */
    _tzset();

    /* Display operating system-style date and time. */
    _strtime( tmpbuf );
    printf( "OS time:\t\t\t\t%s\n", tmpbuf );
    _strdate( tmpbuf );
    printf( "OS date:\t\t\t\t%s\n", tmpbuf );

    /* Get UNIX-style time and display as number and string. */
    time( &ltime );
    printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );
    printf( "UNIX time and date:\t\t\t%s", ctime( &ltime ) );

    /* Display UTC. */
    gmt = gmtime( &ltime );
    printf( "Coordinated universal time:\t\t%s", asctime( gmt ) );

    /* Convert to time structure and adjust for PM if necessary. */
    today = localtime( &ltime );
    if( today->tm_hour > 12 )
    {
   strcpy( ampm, "PM" );
   today->tm_hour -= 12;
    }
    if( today->tm_hour == 0 )  /* Adjust if midnight hour. */
   today->tm_hour = 12;

    /* Note how pointer addition is used to skip the first 11 
     * characters and printf is used to trim off terminating 
     * characters.
     */
    printf( "12-hour time:\t\t\t\t%.8s %s\n",
       asctime( today ) + 11, ampm );

    /* Print additional time information. */
    _ftime( &tstruct );
    printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm );
    printf( "Zone difference in seconds from UTC:\t%u\n", 
             tstruct.timezone );
    printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] );
    printf( "Daylight savings:\t\t\t%s\n", 
             tstruct.dstflag ? "YES" : "NO" );

    /* Make time for noon on Christmas, 1993. */
    if( mktime( &xmas ) != (time_t)-1 )
   printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) );

    /* Use time structure to build a customized time string. */
    today = localtime( &ltime );

    /* Use strftime to build a customized time string. */
    strftime( tmpbuf, 128,
         "Today is %A, day %d of %B in the year %Y.\n", today );
    printf( tmpbuf );
}


Output

OS time:                                21:51:03
OS date:                                05/03/94
Time in seconds since UTC 1/1/70:       768027063
UNIX time and date:                     Tue May 03 21:51:03 1994
Coordinated universal time:             Wed May 04 04:51:03 1994
12-hour time:                           09:51:03 PM
Plus milliseconds:                      279
Zone difference in seconds from UTC:    480
Time zone name:                         
Daylight savings:                       YES
Christmas                               Sat Dec 25 12:00:00 1993

Today is Tuesday, day 03 of May in the year 1994.




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Joda-Time 是一个 Java 的日期和时间处理类库,它提供了比 Java 标准库更加丰富的 API,使用起来更加方便。 以下是 Joda-Time 的使用方法: 1. 引入 Joda-Time 的依赖 在 Maven 项目中,可以在 `pom.xml` 文件中添加以下依赖: ```xml <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.10.13</version> </dependency> ``` 2. 创建日期和时间对象 Joda-Time 中的日期和时间对象分别为 `LocalDate` 和 `LocalTime`。可以使用静态方法 `now()` 来获取当前日期和时间: ```java LocalDate currentDate = LocalDate.now(); LocalTime currentTime = LocalTime.now(); ``` 也可以通过传入年月日、时分秒等参数来创建指定日期和时间对象: ```java LocalDate date = new LocalDate(2021, 9, 14); LocalTime time = new LocalTime(12, 30, 0); ``` 3. 日期和时间的加减 Joda-Time 中可以方便地对日期和时间进行加减操作。例如,可以通过 `plusDays()` 方法来增加指定的天数: ```java LocalDate newDate = currentDate.plusDays(7); ``` 也可以通过 `minusHours()` 方法来减少指定的小时数: ```java LocalTime newTime = currentTime.minusHours(3); ``` 4. 日期和时间的格式化 Joda-Time 中可以使用 `DateTimeFormatter` 类来进行日期和时间的格式化。例如,可以使用 `ISODateTimeFormat` 来格式化日期和时间: ```java DateTimeFormatter formatter = ISODateTimeFormat.dateTime(); String formattedDate = formatter.print(currentDate); String formattedTime = formatter.print(currentTime); ``` 也可以自定义日期和时间的格式化方式。例如,可以使用 `DateTimeFormat` 来指定格式化方式: ```java DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); String formattedDate = formatter.print(currentDate); String formattedTime = formatter.print(currentTime); ``` 以上就是 Joda-Time 的基本使用方法。需要注意的是,Joda-Time 在 Java 8 之后已经被废弃,建议使用 Java 8 中提供的 `java.time` 包来处理日期和时间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值