在JSP页面将时间戳转换成格式化日期的集中方法

A.

       在网上找过很多资料,感觉都没有直接由时间戳转换成格式化的方法,最后想到一个方法,通过Java.util.Date类将时间戳换算成日期。

做法:通过标签引入java.util.Date日期类,再使用标签将时间戳设置到Date的time属性中,最后通过

格式化时间输出




B.

datetag.tld文件:
    

PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

1.0
1.2
date
http://com.demo.util.datejstl/tags

date
com.demo.util.jstl.JSTLUtil

value
true
true


parttern
true
true




在Jsp页面中引用:
    
<%@ taglib uri="http://com.demo.util.datejstl/tags" prefix="date"%>

在Jsp页面中用法:
    

C.

以毫秒为整数值的时间戳转换

    时间戳转化为时间NSDate

- (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}

    时间转化为时间戳

   
     // 当前时间
     NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[date timeIntervalSince1970]*1000; // *1000 是精确到毫秒,不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", a]; //转为字符型
    

    通过比较时间与当前时间返回年月日的方法

- (void)getBabyDetailAge:(NSString *)date
{
    // 获得日期对象
    NSDateFormatter *formatter_ = [[NSDateFormatter alloc] init];
    formatter_.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *createDate = [formatter_ dateFromString:date];
    
    NSCalendar *gregorian = [[ NSCalendar alloc ] initWithCalendarIdentifier : NSCalendarIdentifierGregorian];
    NSUInteger unitFlags = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear;
    NSDateComponents *components = [gregorian components:unitFlags fromDate:createDate toDate:[NSDate date] options: 0 ];
    
    NSInteger years = [components year];
    NSInteger months = [components month ];
    NSInteger days = [components day ];
}

你可能感兴趣的:(JSP_时间格式化)