Scala之Calendar,SimpleDateFormat简单用法

 1 package com.dingxin.entrance
 2 
 3 import java.text.SimpleDateFormat
 4 import java.util.{Calendar, Date}
 5 
 6 /**
 7   * Created by zhen on 2019/1/16.
 8   */
 9 object SimpleDateFormatTest {
10   def main(args: Array[String]) {
11     val now: Date = new Date()
12     val cal = Calendar.getInstance()
13     val cbzq = "3"
14     val result = if(cbzq == "1"){ //当年1月至当年12月
15       val dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-01-01")
16       val begin = dateFormat.format(now)
17 
18       cal.add(Calendar.YEAR,1)
19       val end = dateFormat.format(cal.getTime)
20 
21       begin.toString +"_"+end.toString
22     }else if(cbzq == "2"){ //上年12月至当年12月
23       val dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-12-01")
24       val end = dateFormat.format(now)
25 
26       cal.add(Calendar.YEAR,-1)
27       val begin = dateFormat.format(cal.getTime)
28 
29       begin.toString +"_"+end.toString
30     }else if(cbzq == "3") {//上年11月至当年11月
31       val dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-11-01")
32       val end = dateFormat.format(now)
33 
34       cal.add(Calendar.YEAR,-1)
35       val begin = dateFormat.format(cal.getTime)
36 
37       begin.toString +"_"+end.toString
38     }else{// 异常
39       ""
40     }
41     println(result)
42     // 根据字符串时间求时间差(天)
43     val dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd")
44     val begin = dateFormat.parse("2019-11-3").getTime
45     val end = dateFormat.parse("2019-12-27").getTime
46     println((end - begin) / (1000 * 60 * 60 * 24))
47   }
48 }
1     // 计算当前年份的总天数
2     val nowCalendar = Calendar.getInstance()
3     val nowDateFormat : SimpleDateFormat = new SimpleDateFormat("yyyy-12-31")
4     val nowEnd = nowDateFormat.parse(nowDateFormat.format(nowCalendar.getTime)).getTime
5     nowCalendar.add(Calendar.YEAR, -1)
6     val nowBegin = nowDateFormat.parse(nowDateFormat.format(nowCalendar.getTime)).getTime
7 
8     println((nowEnd - nowBegin) / (1000 * 60 * 60 * 24))

结果1:

 结果2:

 

转载于:https://www.cnblogs.com/yszd/p/10280656.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值