日期和时间处理

日期和时间处理是C#编程最为常见的操作之一。与日期相关的的结构有DateTime和TimeSpan。DateTime结构表示时间上的某一刻,TimeSpan结构表示时间间隔。
DateTime结构:
DateTime是一个结构,且为值类型,表示时间上的某一刻,包括日期和时间两部分,由年、月、日、时、分、秒,以及间隔符组成。最常用的格式:YYYY-MM-DD hh:mm:ss
其中,YYYY表示年,四位数字;MM表示月,两位数字;DD表示天,两位数字;hh表示小时,mm表示分钟,ss表示秒。DateTime结构只包含两个静态字段:MaxValue(DateTime最大值)和MinValue(DateTime最小值)
实例MaxValue:获取DateTime结构的MaxValue字段的值,并保存为maxdb变量,变量的值为9999年12月31日23:59:59.9999999.
DateTime maxdb = DateTime.Now.MaxValue;
获取当前时间,并保存为time变量
DateTime time = DateTime.Now.time;
获取当前时间的年份(类型为int),并保存为year变量
Int year = DateTime.Now.Year;
获取当前时间的月份(类型为int),并保存为month变量
Int month = DateTime.Now.Month;
获取当前时间的日期(即该月的第几天,类型为int),并保存为date变量
Int date = DateTime.Now.Date;
获取当前日期是星期几(类型为DayofWeek),并保存为week变量。Dayofweek为枚举类型,包括七个值Monday、Tuesday、Wednesday、Thursday、Friday、 Saturday和Sunday,分别表示星期一到星期天。
DayofWeek week = DateTime.Now.DateTime;

TimeSpan结构:
TimeSpan结构表示一个时间间隔或持续时间,可以按正负天数、小时数、分钟数、秒数以及秒的小数部分进行度量。
创建一个为db的TimeSpan对象,它的天数为1、小时部分为1、分钟部分为2、秒部分为3、毫秒部分为40。Final ts object has a value of 1.1:2:3. 0400000.
TimeSpan = new TimeSpan (1,1,2,3,40);
TimeSpan结构包含以下8个静态字段,如MaxValue、MinValue等
MaxValue字段:最大的TimeSpan值,它的值为+10675199.02:48:05.4775807。
MinValue字段:最小的TimeSpan值,它的值为-10675199.02:48:05.4775808。
Zero字段:零TimeSpan值,它的值为00:00:00。常常用于与其他TimeSpan对象的比较。
TicksPerDay字段:一天中的刻度数,它的值为8640亿。
TicksPerHour字段: 1小时的刻度数,它的值为360亿。
TicksPerMinute字段: 1分钟的刻度数,它的值为6亿。
TicksPerSecond字段: 1秒钟的刻度数,它的值为1000万。
获取TimeSpan结构的MaxValue字段的值,并保存为maxts变量。此时,maxdt变量的值为+10675199.02:48:05.4775807。
TimeSpan maxts = TimeSpan. MaxValue:

由于日期不能进行计算函数,所以日期函数就十分有用了。
GATEDATE()当前系统日期
Select getedate() --结果:当前系统日期
DATEDIFF(日期部分,date1,date2) 返回两个日期中指定的日期部分之间的差值
Select Datediff(mm,””,””)–结果:两个日期的时间差
DATENAME(日期部分,date) 返回日期中日期部分的字符串形式。
select DATENAME(dw,GETDATE()) --结果:星期一
DATEPART(日期部分,date) 返回日期中指定的日期部分的整数形式。
select DATEPART(dw,GETDATE()) --结果(返回今天是一周中的第几天):2

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Java程序,用于获取当前日期时间,以及执行一些基本的日期时间操作: ```java import java.time.LocalDate; import java.time.LocalTime; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class DateTimeExample { public static void main(String[] args) { // 获取当前日期时间 LocalDate currentDate = LocalDate.now(); LocalTime currentTime = LocalTime.now(); LocalDateTime currentDateTime = LocalDateTime.now(); // 打印当前日期时间 System.out.println("Current Date: " + currentDate); System.out.println("Current Time: " + currentTime); System.out.println("Current Date and Time: " + currentDateTime); // 格式化日期时间 DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss"); DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"); String formattedDate = currentDate.format(dateFormatter); String formattedTime = currentTime.format(timeFormatter); String formattedDateTime = currentDateTime.format(dateTimeFormatter); // 打印格式化后的日期时间 System.out.println("Formatted Date: " + formattedDate); System.out.println("Formatted Time: " + formattedTime); System.out.println("Formatted Date and Time: " + formattedDateTime); // 执行日期时间操作 LocalDate tomorrow = currentDate.plusDays(1); LocalDate lastWeek = currentDate.minusWeeks(1); LocalTime twoHoursLater = currentTime.plusHours(2); // 打印执行操作后的日期时间 System.out.println("Tomorrow's Date: " + tomorrow); System.out.println("Last Week's Date: " + lastWeek); System.out.println("Two Hours Later: " + twoHoursLater); } } ``` 此程序使用Java 8的日期时间API。它获取当前日期时间,并使用格式化程序将其格式化为指定的格式。它还执行一些基本的日期时间操作,例如添加一天,减去一周,以及添加两个小时。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值