#import <Foundation/Foundation.h>
@interface CalculatorSingleton : NSObject
@property (nonatomic, strong) NSCalendar *calendar;
+ (CalculatorSingleton *) sharedInstance;
- (int)numberOfWeeksInMonthContainingDate:(NSDate *)date;
- (NSDate *)firstDayOfMonthContainingDate:(NSDate *)date;
- (NSDate *)lastDayOfMonthContainingDate:(NSDate *)date;
- (BOOL)dateIsToday:(NSDate *)date;
- (NSDate*)getLocaleDate;
+ (NSDate*)getLocaleDate;
- (int)dayOfWeekForDate:(NSDate *)date;
- (NSDate *)nextDay:(NSDate *)date;
- (NSDate*)previousDay:(NSDate*)date;
- (NSDate*)nextMonth:(NSDate*)date;
- (NSDate*)previousMonth:(NSDate*)date;
- (BOOL)date:(NSDate*)date1 InSameMonthWith:(NSDate*)date2;
- (int)weekNumberInMonthForDate:(NSDate *)date;
- (NSString*)formatDate:(NSDate*)date;
- (int)year:(NSDate*)date;
- (int)month:(NSDate*)date;
- (int)day:(NSDate*)date;
- (int)hour:(NSDate*)date;
- (int)minute:(NSDate*)date;
- (int)second:(NSDate*)date;
- (NSString*)senselFormatDate:(NSDate*)date;
- (NSString*)HMS:(NSDate*)date;
-(NSDate *)nextOrPreviousDay:(NSDate *)date day:(NSInteger)day;
- (NSDate*)localToGMT:(NSDate*)date;
-(NSDate *)GMTToLacal:(NSDate *)date;
- (BOOL)isSameDay:(NSDate*)oneDate withAnotherDay:(NSDate*)anotherDate;
- (NSString*)getChineseCalendarWithDate:(NSDate *)date;
- (NSString*)getMemoryLunarShow;
- (NSDate*)nextYear:(NSDate*)date;
- (NSDate*)previousYear:(NSDate*)date;
@end
#import "CalculatorSingleton.h"
@implementation CalculatorSingleton
@synthesize calendar=_calendar;
staticCalculatorSingleton *_sharedInstance = nil;
//类方法
+ (CalculatorSingleton*)sharedInstance
{
if (_sharedInstance != nil) {
return_sharedInstance;
}
@synchronized(self) {
if (_sharedInstance == nil) {
[[[selfalloc] init] autorelease];
}
}
return_sharedInstance;
}
+ (id)allocWithZone:(NSZone*)zone
{
@synchronized(self) {
if (_sharedInstance == nil) {
_sharedInstance = [superallocWithZone:zone];
return_sharedInstance;
}
}
NSAssert(NO, @ "[BlockBackground alloc] explicitly called on singleton class.");
returnnil;
}
- (id)copyWithZone:(NSZone*)zone
{
returnself;
}
- (id)retain
{
returnself;
}
-(unsigned)retainCount
{
returnUINT_MAX;
}
-(onewayvoid)release
{
}
-(id)autorelease
{
returnself;
}
#pragma mark -
-(id)init
{
self=[superinit];
if(self){
// NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]autorelease];
// NSLog(@"[NSLocale systemLocale]=%@",[NSLocale systemLocale]);
// //[calendar setLocale:[NSLocale systemLocale]];
// [self.calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
// [calendar setFirstWeekday:2];//从周一开始
// self.calendar = calendar;
// NSDate* today = [self getLocaleDate];
// NSLog(@"%@",today);
self.calendar=[[[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar]autorelease];
[self.calendarsetLocale:[NSLocalesystemLocale]];
[self.calendarsetTimeZone:[NSTimeZonetimeZoneWithAbbreviation:@"GMT"]];
[self.calendarsetFirstWeekday:1];//从周日开始
NSDate* today =[selfgetLocaleDate];
NSLog(@"今天是%@",today);
}
returnself;
}
-(void)dealloc
{
[_calendarrelease];
[superdealloc];
}
#pragma mark -根据某天返回这天所在月有几个星期
-(int)numberOfWeeksInMonthContainingDate:(NSDate*)date {
return[self.calendarrangeOfUnit:NSWeekCalendarUnitinUnit:NSMonthCalendarUnitforDate:date].length;
}
#pragma mark -根据某天得到本天所在月的第一天
-(NSDate*)firstDayOfMonthContainingDate:(NSDate*)date {
NSDateComponents*comps =[self.calendarcomponents:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit)fromDate:date];
[comps setDay:1];
NSDate* returnDate =[self.calendardateFromComponents:comps];
return returnDate;
}
#pragma mark -根据某天得到本天所在月的最后一天
-(NSDate*)lastDayOfMonthContainingDate:(NSDate*)date {
// NSDateComponents *comps = [self.calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date];
// [comps setMonth:1];
// [comps setDay:0];
// NSDate* returnDate = [self.calendar dateFromComponents:comps];
// return returnDate;
NSDate* firstDay=[selffirstDayOfMonthContainingDate:date];
NSDate* nextMonthFirstDay=[selfnextMonth:firstDay];
return[selfpreviousDay:nextMonthFirstDay];
}
#pragma mark -判断是不是今天
-(BOOL)dateIsToday:(NSDate*)date {
NSDateComponents*otherDay =[self.calendarcomponents:NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnitfromDate:date];
NSDateComponents*today =[self.calendarcomponents:NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnitfromDate:[selfgetLocaleDate]];
BOOL isToday=([today day]==[otherDay day]&&
[today month]==[otherDay month]&&
[today year]==[otherDay year]&&
[today era]==[otherDay era]);
//NSLog(@"date=%@,isToday=%d",date,isToday);
return isToday;
}
#pragma mark -获取本地时间
-(NSDate*)getLocaleDate{
NSDate*date =[NSDatedate];
NSTimeZone*zone =[NSTimeZonesystemTimeZone];
NSInteger interval =[zone secondsFromGMTForDate: date];
NSDate* realDate =[date dateByAddingTimeInterval: interval];
return realDate;
}
-(NSDate*)GMTToLocal{
NSDate* date =[[NSDatedate]dateByAddingTimeInterval:[[NSTimeZonesystemTimeZone]secondsFromGMT]];
return date;
}
-(NSDate*)localToGMT:(NSDate*)date{
return[date dateByAddingTimeInterval:-[[NSTimeZonesystemTimeZone]secondsFromGMT]];
}
-(NSDate*)GMTToLacal:(NSDate*)date{
return[date dateByAddingTimeInterval:[[NSTimeZonesystemTimeZone]secondsFromGMT]];
}
+(NSDate*)getLocaleDate{
NSDate*date =[NSDatedate];
NSTimeZone*zone =[NSTimeZonesystemTimeZone];
NSInteger interval =[zone secondsFromGMTForDate: date];
NSDate* realDate =[date dateByAddingTimeInterval: interval];
return realDate;
}
#pragma mark -根据某天返回这一天是星期几
-(int)dayOfWeekForDate:(NSDate*)date {
NSDateComponents*comps =[self.calendarcomponents:NSWeekdayCalendarUnitfromDate:date];
// NSLog(@"comps.weekday=%d",comps.weekday);
return comps.weekday;
}
-(int)weekNumberInMonthForDate:(NSDate*)date {
NSDateComponents*comps =[self.calendarcomponents:(NSWeekOfMonthCalendarUnit)fromDate:date];
return comps.weekOfMonth;
}
#pragma mark -获取date的昨天,明天,下个月,上个月
-(NSDate*)nextDay:(NSDate*)date {
NSDateComponents*comps =[[NSDateComponentsalloc]init];
[comps setDay:1];
NSDate* returnDate =[self.calendardateByAddingComponents:comps toDate:date options:0];
[comps release];
return returnDate;
}
-(NSDate*)nextOrPreviousDay:(NSDate*)date day:(NSInteger)day{
NSDateComponents*comps =[[[NSDateComponentsalloc]init]autorelease];
[comps setDay:day];
return[self.calendardateByAddingComponents:comps toDate:date options:0];
}
-(NSDate*)previousDay:(NSDate*)date{
NSDateComponents*comps =[[[NSDateComponentsalloc]init]autorelease];
[comps setDay:-1];
return[self.calendardateByAddingComponents:comps toDate:date options:0];
}
-(NSDate*)nextMonth:(NSDate*)date{
NSDateComponents*comps =[[[NSDateComponentsalloc]init]autorelease];
[comps setMonth:1];
return[self.calendardateByAddingComponents:comps toDate:date options:0];
}
-(NSDate*)nextYear:(NSDate*)date{
NSDateComponents*comps =[[[NSDateComponentsalloc]init]autorelease];
[comps setYear:1];
return[self.calendardateByAddingComponents:comps toDate:date options:0];
}
-(NSDate*)previousYear:(NSDate*)date{
NSDateComponents*comps =[[[NSDateComponentsalloc]init]autorelease];
[comps setYear:-1];
return[self.calendardateByAddingComponents:comps toDate:date options:0];
}
-(NSDate*)previousMonth:(NSDate*)date{
NSDateComponents*comps =[[[NSDateComponentsalloc]init]autorelease];
[comps setMonth:-1];
return[self.calendardateByAddingComponents:comps toDate:date options:0];
}
#pragma mark -
-(BOOL)date:(NSDate*)date1 InSameMonthWith:(NSDate*)date2{
NSDateComponents*comps1 =[self.calendarcomponents:(NSMonthCalendarUnit)fromDate:date1];
NSDateComponents*comps2 =[self.calendarcomponents:(NSMonthCalendarUnit)fromDate:date2];
return comps1.month== comps2.month;
}
#pragma mark -时间格式化NSDate->2012-02-02
-(NSString*)senselFormatDate:(NSDate*)date{
int year =[selfyear:date];
int mouth =[selfmonth:date];
int day =[selfday:date];
NSString* dateString =[[NSStringalloc]initWithFormat:@"%.2d/%.2d/%.2d",year,mouth,day];
return[dateString autorelease];
}
-(NSString*)HMS:(NSDate*)date{
int hour =[selfhour:date];
int minute =[selfminute:date];
int second =[selfsecond:date];
NSString* hmss =[[NSStringalloc]initWithFormat:@"%.2d:%.2d:%.2d",hour,minute,second];
return[hmss autorelease];
}
-(NSString*)formatDate:(NSDate*)date{
int year =[selfyear:date];
int mouth =[selfmonth:date];
int day =[selfday:date];
NSString* dateString =[[NSStringalloc]initWithFormat:@"%.2d-%.2d-%.2d",year,mouth,day];
return[dateString autorelease];
}
-(int)year:(NSDate*)date {
NSDateComponents*components =[self.calendarcomponents:NSYearCalendarUnitfromDate:date];
return[components year];
}
-(int)month:(NSDate*)date {
NSDateComponents*components =[self.calendarcomponents:NSMonthCalendarUnitfromDate:date];
return[components month];
}
-(int)day:(NSDate*)date {
NSDateComponents*components =[self.calendarcomponents:NSDayCalendarUnitfromDate:date];
return[components day];
}
-(int)hour:(NSDate*)date{
NSDateComponents*components =[self.calendarcomponents:NSHourCalendarUnitfromDate:date];
return[components hour];
}
-(int)minute:(NSDate*)date{
NSDateComponents*components =[self.calendarcomponents:NSMinuteCalendarUnitfromDate:date];
return[components minute];
}
-(int)second:(NSDate*)date{
NSDateComponents*components =[self.calendarcomponents:NSSecondCalendarUnitfromDate:date];
return[components second];
}
-(BOOL)isSameDay:(NSDate*)oneDate withAnotherDay:(NSDate*)anotherDate{
int oneYear=[selfyear:oneDate];
int anotherYear=[selfyear:anotherDate];
int oneMonth=[selfmonth:oneDate];
int anotherMonth=[selfmonth:anotherDate];
int oneDay=[selfday:oneDate];
int anotherDay=[selfday:anotherDate];
if(oneYear==anotherYear && oneMonth==anotherMonth && oneDay==anotherDay){
returnYES;
}else{
returnNO;
}
}
-(NSString*)getChineseCalendarWithDate:(NSDate*)date{
NSArray*chineseMonths=[NSArrayarrayWithObjects:
@"正月",@"二月",@"三月",@"四月",@"五月",@"六月",@"七月",@"八月",
@"九月",@"十月",@"冬月",@"腊月",nil];
NSArray*chineseDays=[NSArrayarrayWithObjects:
@"初一",@"初二",@"初三",@"初四",@"初五",@"初六",@"初七",@"初八",@"初九",@"初十",
@"十一",@"十二",@"十三",@"十四",@"十五",@"十六",@"十七",@"十八",@"十九",@"二十",
@"廿一",@"廿二",@"廿三",@"廿四",@"廿五",@"廿六",@"廿七",@"廿八",@"廿九",@"三十", nil];
NSCalendar*localeCalendar=[[NSCalendaralloc]initWithCalendarIdentifier:NSChineseCalendar];
unsigned unitFlags =NSYearCalendarUnit|NSMonthCalendarUnit| NSDayCalendarUnit;
NSDateComponents*localeComp =[localeCalendar components:unitFlags fromDate:date];
NSString*m_str =[chineseMonths objectAtIndex:localeComp.month-1];//月
NSString*d_str =[chineseDays objectAtIndex:localeComp.day-1];//日
NSDateFormatter*dateFormatter =[[NSDateFormatteralloc]init];
//设定时间格式,这里可以设置成自己需要的格式
[dateFormatter setDateFormat:@"yyyy年MM月dd日"];
NSString*currentDateStr =[dateFormatter stringFromDate:[NSDatedate]];
NSString*chineseCal_str =[NSStringstringWithFormat:@"%@ 农历%@%@",currentDateStr,m_str,d_str];
[localeCalendar release];
[dateFormatter release];
return chineseCal_str;
}
-(NSString*)getMemoryLunarShow{
NSArray*chineseMonths=[NSArrayarrayWithObjects:
@"正月",@"二月",@"三月",@"四月",@"五月",@"六月",@"七月",@"八月",
@"九月",@"十月",@"冬月",@"腊月",nil];
NSArray*chineseDays=[NSArrayarrayWithObjects:
@"初一",@"初二",@"初三",@"初四",@"初五",@"初六",@"初七",@"初八",@"初九",@"初十",
@"十一",@"十二",@"十三",@"十四",@"十五",@"十六",@"十七",@"十八",@"十九",@"二十",
@"廿一",@"廿二",@"廿三",@"廿四",@"廿五",@"廿六",@"廿七",@"廿八",@"廿九",@"三十", nil];
NSArray* weeks =[NSArrayarrayWithObjects:@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",nil];
NSDate* today =[selfgetLocaleDate];
NSCalendar*localeCalendar =[[NSCalendaralloc]initWithCalendarIdentifier:NSChineseCalendar];
unsigned unitFlags =NSYearCalendarUnit|NSMonthCalendarUnit| NSDayCalendarUnit;
NSDateComponents*localeComp =[localeCalendar components:unitFlags fromDate:today];
NSString*m_str =[chineseMonths objectAtIndex:localeComp.month-1];//月
NSString *d_str = [chineseDays objectAtIndex:localeComp.day-1];//日
int month = [selfmonth:today];
int day =[selfday:today];
int weekNum=[selfdayOfWeekForDate:today];
NSString* week =[NSStringstringWithFormat:@"%@",[weeks objectAtIndex:weekNum-1]];
return [NSStringstringWithFormat:@"农历%@%@ %d月%d日(%@)",m_str,d_str,month,day,week];
}
@end
相关推荐
易语言是一种专为中国人设计的编程语言,它以简体...通过研究源码,不仅可以学习到如何使用日期时间框控件,还能了解到易语言中关于日期时间处理的基本方法和技巧,这对于提升编程能力、扩展项目功能都有很大的帮助。
在编程中,处理时间和日期是一项常见的任务,无论是记录系统时间、计算时间差还是格式化显示,都需要用到相关函数和方法。易语言提供了丰富的内置命令来支持这些操作。例如,我们可以使用“取当前时间”命令获取系统...
此外,Java 8引入了新的日期时间API,包括`java.time.LocalDate`、`java.time.LocalDateTime`和`java.time.format.DateTimeFormatter`等类,它们提供了更强大且易于使用的日期时间处理功能。虽然`SimpleDateFormat`...
在Windows系统中,常规的更改日期时间格式的方法是通过“控制面板”进入“区域和语言”选项,然后在“日期、时间和数字”部分进行调整。然而,这个过程可能对一些不熟悉计算机操作的用户来说较为复杂。通过编写VB...
2. **日期时间选择器组件** 在网页上,用户友好地选择日期通常需要一个交互式的日期选择器。JavaScript库如jQuery UI的`datepicker`插件,Bootstrap的`datetimepicker`,或者现代的React组件库如Material-UI的`...
需要注意的是,Java 8 引入了 `java.time` 包,其中的 `LocalDate`, `LocalTime`, `LocalDateTime` 等类提供了更强大且易用的日期时间处理功能,推荐在新项目中优先使用。然而,对于已经存在的代码或需要兼容旧版本 ...
掌握这些基本操作对任何编程工作都至关重要,无论是数据分析、后端开发还是前端交互,都会频繁地用到字符串和日期处理。通过实践和理解这些方法,开发者能够更高效地处理数据和信息,提升代码的可读性和效率。
总之,理解和熟练掌握Java的日期时间操作对于开发人员来说至关重要,无论是在处理用户输入、记录日志还是进行复杂的业务逻辑计算中,都可能需要用到这些功能。通过学习和实践,你可以编写出更加高效和易维护的代码。
### JavaScript 日期函数库知识点详解 #### 一、引言 在JavaScript开发中,处理日期时间是一项常见的需求。...这些功能覆盖了日常开发中常见的日期时间处理需求,通过使用这些方法,可以大大简化代码并提高开发效率。
在网页中,处理日期和时间的选择是常见的需求,这通常涉及到创建一个用户友好的日期选择器或时间选择器。本文将深入探讨JavaScript中的日期和时间选择器的实现及其相关知识点。 1. **JavaScript Date对象** ...
2. **toString()方法**: Date对象有一个内置的`toString()`方法,它会返回一个易于阅读的日期和时间字符串。但这个格式可能不符合所有场景的需求。 3. **format化函数**: `date.format.js`、`DateFormat.js`和`...
2. **API方法**:插件可能提供一个方法来改变默认日期,如`setDefaultDate`: ```javascript datePlugin.setDefaultDate('2022-12-25'); ``` 这个调用后,插件的默认日期会被更新为圣诞节。 3. **事件监听**:...
`Calendar`类提供了日期和时间的抽象表示,而`java.time`包引入了Java 8的日期和时间API,如`LocalDate`、`LocalTime`和`LocalDateTime`,提供了更易用和强大的日期时间操作。 在实际应用中,你可能还需要考虑以下...
在前端开发中,日期和时间控件是必不可少的元素,它们通常用于日历选择、时间选择或者日期时间的组合输入,提升用户体验并确保数据输入的准确性。".js带时间的日期控件"就是这样一个专门针对日期和时间选择的组件。 ...
4. **自定义特效**:源码可能包含了对这些插件的定制,通过jQuery的动画API(如`.fadeIn()`, `.slideToggle()`等)实现动态展示和隐藏日期时间组件,或者使用CSS3动画增强视觉效果。 5. **事件绑定**:jQuery的`.on...
在学习和使用这段源码时,开发者可以深入理解日期和时间的处理方法,以及如何在用户界面中实现动态更新。同时,这也是提高编程技能和理解程序流程的好机会。对于想要开发自己的日历或时间显示应用的人来说,这个源码...
在iOS开发中,日期和时间的格式化是一个常见的任务,特别是在显示用户友好的时间戳时。"iOS日期格式化类(刚刚、一分钟前、昨天、周几)"这个主题聚焦于如何利用Objective-C来处理日期的本地化显示,使得时间表述更加...
此外,Java 8引入的`java.time`包还提供了很多实用方法,如计算两个日期之间的天数差、判断日期是否在另一日期之前或之后等,这大大简化了日期和时间的处理。 源程序夹中的`time`文件很可能包含了上述类的实例化、...
Delphi 日期时间函数详解 Delphi 中的日期和时间函数是编程中非常重要的一部分,它可以帮助开发者更好地处理日期和时间相关的操作。下面是 Delphi 中一些常用的日期和时间函数的详解。 DateTimeToFileDate 函数 ...
4. **日期时间处理**:JavaScript中的`Date`对象是处理日期和时间的基础,需要了解如何创建新的Date实例,获取和设置年、月、日、小时、分钟等属性,以及格式化日期字符串的方法。 5. **表格渲染**:根据当前日期...