一个显示日期的工具类

.h文件

#import <Foundation/Foundation.h>

@interface TimeUtil : NSObject

+ (NSString*)getTimeStr1:(long long)time;

+(NSString*) getTimeStrStyle1:(long long)time;
+ (NSString*)getTimeStr1Short:(long long)time;

+(NSString*) getTimeStrStyle2:(long long)time;

+(int)dayCountForMonth:(int)month andYear:(int)year;

+(BOOL)isLeapYear:(int)year;

@end

.m文件

#import "TimeUtil.h"

@implementation TimeUtil

+ (NSString*)getTimeStr:(long) createdAt
{
    // Calculate distance time string
    //
    NSString *timestamp;
    time_t now;
    time(&now);

    int distance = (int)difftime(now, createdAt);
    if (distance < 0) distance = 0;

    if (distance < 60) {
        timestamp = [NSString stringWithFormat:@"%d %s", distance, (distance == 1) ? "second ago" : "seconds ago"];
    }
    else if (distance < 60 * 60) {
        distance = distance / 60;
        timestamp = [NSString stringWithFormat:@"%d %s", distance, (distance == 1) ?

"minute ago" : "minutes ago"];
    }
    else if (distance < 60 * 60 * 24) {
        distance = distance / 60 / 60;
        timestamp = [NSString stringWithFormat:@"%d %s", distance, (distance == 1) ? "hour ago" : "hours ago"];
    }
    else if (distance < 60 * 60 * 24 * 7) {
        distance = distance / 60 / 60 / 24;
        timestamp = [NSString stringWithFormat:@"%d %s", distance, (distance == 1) ? "day ago" : "days ago"];
    }
    else if (distance < 60 * 60 * 24 * 7 * 4) {
        distance = distance / 60 / 60 / 24 / 7;
        timestamp = [NSString stringWithFormat:@"%d %s", distance, (distance == 1) ? "week ago" : "weeks ago"];
    }
    else {
        static NSDateFormatter *dateFormatter = nil;
        if (dateFormatter == nil) {
            dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateStyle:NSDateFormatterShortStyle];
            [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
        }

        NSDate *date = [NSDate dateWithTimeIntervalSince1970:createdAt];
        timestamp = [dateFormatter stringFromDate:date];
    }
    return timestamp;
}

+ (NSString*)getTimeStr1:(long long)time
{
    NSDate * date=[NSDate dateWithTimeIntervalSince1970:time];
    NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    NSInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
    NSDateComponents * component=[calendar components:unitFlags fromDate:date];
    NSString * string=[NSString stringWithFormat:@"%04d-%02d-%02d %02d:%02d",[component year],[component month],[component day],[component hour],[component minute]];
    return string;
}

+ (NSString*)getTimeStr1Short:(long long)time
{
    NSDate * date=[NSDate dateWithTimeIntervalSince1970:time];
    NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    NSInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
    NSDateComponents * component=[calendar components:unitFlags fromDate:date];
    NSString * string=[NSString stringWithFormat:@"%04d-%02d-%02d",[component year],[component month],[component day]];
    return string;
}

+ (NSString*)getMDStr:(long long)time
{

    NSDate * date=[NSDate dateWithTimeIntervalSince1970:time];
    NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    NSInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
    NSDateComponents * component=[calendar components:unitFlags fromDate:date];
    NSString * string=[NSString stringWithFormat:@"%d月%d日",[component month],[component day]];
    return string;
}

+(NSDateComponents*) getComponent:(long long)time
{
    NSDate * date=[NSDate dateWithTimeIntervalSince1970:time];
    NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    NSInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
    NSDateComponents * component=[calendar components:unitFlags fromDate:date];
    return component;
}

+(NSString*) getTimeStrStyle1:(long long)time
{
    NSDate * date=[NSDate dateWithTimeIntervalSince1970:time];
    NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    NSInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit;
    NSDateComponents * component=[calendar components:unitFlags fromDate:date];

    int year=[component year];
    int month=[component month];
    int day=[component day];

    int hour=[component hour];
    int minute=[component minute];

    NSDate * today=[NSDate date];
    component=[calendar components:unitFlags fromDate:today];

    int t_year=[component year];

    NSString*string=nil;

    long long now=[today timeIntervalSince1970];

    long long distance=now-time;
    if(distance<60)
        [email protected]"刚刚";
    else if(distance<60*60)
        string=[NSString stringWithFormat:@"%lld分钟前",distance/60];
    else if(distance<60*60*24)
        string=[NSString stringWithFormat:@"%lld小时前",distance/60/60];
    else if(distance<60*60*24*7)
        string=[NSString stringWithFormat:@"%lld天前",distance/60/60/24];
    else if(year==t_year)
        string=[NSString stringWithFormat:@"%02d-%02d %d:%02d",month,day,hour,minute];
    else
        string=[NSString stringWithFormat:@"%d-%d-%d",year,month,day];

    return string;   

}
+(NSString*) getTimeStrStyle2:(long long)time
{

    NSDate * date=[NSDate dateWithTimeIntervalSince1970:time];
    NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    NSInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit;
    NSDateComponents * component=[calendar components:unitFlags fromDate:date];

    int year=[component year];
    int month=[component month];
    int day=[component day];
    int hour=[component hour];
    int minute=[component minute];
    int week=[component week];
    int weekday=[component weekday];

    NSDate * today=[NSDate date];
    component=[calendar components:unitFlags fromDate:today];

    int t_year=[component year];
    int t_month=[component month];
    int t_day=[component day];
    int t_week=[component week];

    NSString*string=nil;
    if(year==t_year&&month==t_month&&day==t_day)
    {
        if(hour<6&&hour>=0)
            string=[NSString stringWithFormat:@"凌晨 %d:%02d",hour,minute];
        else if(hour>=6&&hour<12)
            string=[NSString stringWithFormat:@"上午 %d:%02d",hour,minute];
        else if(hour>=12&&hour<18)
            string=[NSString stringWithFormat:@"下午 %d:%02d",hour,minute];
        else
            string=[NSString stringWithFormat:@"晚上 %d:%02d",hour,minute];
    }
    else if(year==t_year&&week==t_week)
    {
        NSString * daystr=nil;
        switch (weekday) {
            case 1:
                [email protected]"日";
                break;
            case 2:
                [email protected]"一";
                break;
            case 3:
                [email protected]"二";
                break;
            case 4:
                [email protected]"三";
                break;
            case 5:
                [email protected]"四";
                break;
            case 6:
                [email protected]"五";
                break;
            case 7:
                [email protected]"六";
                break;
            default:
                break;
        }
        string=[NSString stringWithFormat:@"周%@ %d:%02d",daystr,hour,minute];
    }
    else if(year==t_year)
        string=[NSString stringWithFormat:@"%d月%d日",month,day];
    else
        string=[NSString stringWithFormat:@"%d年%d月%d日",year,month,day];

    return string;
}

+(int)dayCountForMonth:(int)month andYear:(int)year
{
    if (month==1||month==3||month==5||month==7||month==8||month==10||month==12) {
        return 31;
    }else if(month==4||month==6||month==9||month==11){
        return 30;
    }else if([self isLeapYear:year]){
        return 29;
    }else{
        return 28;
    }
}
+(BOOL)isLeapYear:(int)year
{
    if (year%400==0) {
        return YES;
    }else{
        if (year%4==0&&year%100!=0) {
            return YES;
        }else{
            return NO;
        }
    }
}

@end
时间: 2025-01-01 18:57:17

一个显示日期的工具类的相关文章

java高仿微博日期显示格式化,日期辅助工具类

原文:java高仿微博日期显示格式化,日期辅助工具类 源代码下载地址:http://www.zuidaima.com/share/1550463377902592.htm 仿新浪微博日期格式化工具类: 例如: 1. 刚刚 2. xx分钟前 3. hh小时前 4. 今天 HH:mm 5. 昨天 HH:mm 6. 前天 HH:mm 7. 上个月 yyyy-MM-dd HH:mm 8. 去年 yyyy-MM-dd HH:mm 9. 前年 yyyy-MM-dd HH:mm 今天写的代码,结构还很粗糙,只

android日期处理工具类

import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; /** * 日期处理工具类 * Created by Administrator on 2015/10/21 0021. */ public class DateUtils { /** * 根据指定的年.月.日返回当前是星期几.1表示星期天.2表示星期一.7

java日期比较工具类

package com.net.util; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException; /** * 日期比较工具类 * @author zhangdi * */ public class DateUtil { /** *

C# 一个简易的Producer-Consumer工具类

一.关于本文 本文中实现的PCHelper类是一个简易的Producer-Consumer操作工具类.该类可以实现如下目标:由多个线程向其中的一个Queue中写入数据,同时由多个线程负责接收Queue中数据进行处理. 二.工具类代码 /// <summary> /// Producer-Consumer操作类 /// </summary> public class PCHelper {     readonly object listlock = new object(); //线

日期时间工具类

1. 日期格式工具类: package com.ant.jdk8.lang; import org.apache.commons.lang3.time.DateFormatUtils; import java.util.Date; /** * @author wangll * @date 2018/7/17 */ public class DateFormatDemo { public static void main(String[] args) { System.out.println(Da

日期的工具类

import java.util.*;import java.text.*;import java.util.Calendar;/** * * @ClassName: VeDate * @Description: 时间工具类 * @date 2015年7月13日 下午1:23:11 * */public class TimeUtils{ /** * 获取现在时间 * * @return 返回时间类型 yyyy-MM-dd HH:mm:ss */ public static Date getNow

为大家提供一个好的时间工具类DateTime

此类的灵感来源于C# 尽管网上有什么date4j,可是jar太纠结了.先给出源代码,能够继承到自己的util包中.作为一个资深程序猿,我相信都有不少好的util工具类,我也希望经过此次分享,能带动技术大牛们能分享出自己的好用的工具类. 先看源代码 /* * 岸思科技开发平台(JAVA) SDK * * Copyright (c) 2014, Ansitech Network Technology Co.,Ltd All rights reserved. * http://www.ansitech

Android 显示空页面工具类

做项目肯定会遇到这样的需求,数据加载为空或者数据加载异常时候显示一个空页面给用户: 这篇blog就来实现这样一个简单的封装 1.首先定义一个线性布局 2.然后通过一个工具类来管理这样一个View 先来看自定义的线性布局 public class EmptyLayout extends LinearLayout{ /** * 空页面图片 */ private ImageView emptyImg; /** * 空页面文本 */ private TextView emptyTv; /** * 空文字

使用POI做的一个生成Excel的工具类。包含了导出Excel和解析Excel方法

PoiExcelUtils.java /** * */ package com.common.office; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.text.NumberFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.List;