Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala LocalDateTime将字符串转换为ToepochmillisSecond_Scala - Fatal编程技术网

Scala LocalDateTime将字符串转换为ToepochmillisSecond

Scala LocalDateTime将字符串转换为ToepochmillisSecond,scala,Scala,我需要编写一个函数,将任何日期时间字符串转换为Epochmillisond,我已经编写了以下内容 val dateMatch1:Regex = """(\d\d\d\d)-(\d\d)-(\d\d)""".r val timeMatch1:Regex = """(\d\d):(\d\d):(\d\d).(\d\d\d)""".r val timeMatch2:Regex = """(\d\d):(\d\d):(\d\d)""".r val timeMatch3:Regex =

我需要编写一个函数,将任何日期时间字符串转换为Epochmillisond,我已经编写了以下内容

  val dateMatch1:Regex = """(\d\d\d\d)-(\d\d)-(\d\d)""".r

  val timeMatch1:Regex = """(\d\d):(\d\d):(\d\d).(\d\d\d)""".r
  val timeMatch2:Regex = """(\d\d):(\d\d):(\d\d)""".r
  val timeMatch3:Regex = """(\d\d):(\d\d)""".r
  val timeMatch4:Regex = """(\d\d)""".r

  val dateTimeMatch1:Regex = """(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d).(\d\d\d)""".r
  val dateTimeMatch2:Regex = """(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)""".r
  val dateTimeMatch3:Regex = """(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d)""".r
  val dateTimeMatch4:Regex = """(\d\d\d\d)-(\d\d)-(\d\d) (\d\d)""".r


 def stringToTime(data:String): Long = {

    val now = LocalDateTime.now()

    LocalDate.of(now.getYear, now.getMonthValue, now.getDayOfMonth)

    val (year:Int, month:Int, day:Int, hour:Int, minute:Int, second:Int,nanoSecond:Int) = data match {
      case dateMatch1(year, month, day) => (year.toInt, month.toInt, day.toInt, 0, 0, 0, 0)

      case timeMatch1(hour, minute, second,nanoSecond) => (now.getYear, now.getMonthValue, now.getDayOfMonth,hour.toInt, minute.toInt, second.toInt, nanoSecond.toInt * Math.pow(10,6).toInt)
      case timeMatch2(hour, minute, second) =>(now.getYear, now.getMonthValue, now.getDayOfMonth,hour.toInt,hour.toInt, minute.toInt, second.toInt,0)
      case timeMatch3(hour, minute) => (now.getYear, now.getMonthValue, now.getDayOfMonth,hour.toInt,hour.toInt, minute.toInt,0,0)
      case timeMatch4(hour) => (now.getYear, now.getMonthValue, now.getDayOfMonth,hour.toInt,hour.toInt, 0,0,0)

      case dateTimeMatch1(year, month, day, hour, minute, second,nanoSecond) =>(year.toInt, month.toInt, day.toInt, hour.toInt, minute.toInt, second.toInt, nanoSecond.toInt * Math.pow(10,6).toInt)
      case dateTimeMatch2(year, month, day, hour, minute, second) => (year.toInt, month.toInt, day.toInt, hour.toInt, minute.toInt, second.toInt,0)
      case dateTimeMatch3(year, month, day, hour, minute) => (year.toInt, month.toInt, day.toInt, hour.toInt, minute.toInt,0,0)
      case dateTimeMatch4(year, month, day, hour) => (year.toInt, month.toInt, day.toInt, hour.toInt,0,0,0 )

      case _ => (now.getYear,now.getMonthValue,now.getDayOfMonth,0,0,0,0)
    }

    LocalDateTime.of(year, month, day, hour, minute, second,nanoSecond).atZone(ZoneId.systemDefault()).toInstant.toEpochMilli

  }

这很好,但我正在寻找更好的方法。任何人请帮助我

查看Java中的类似问题,大多数答案也应适用于此处。@TzachZohar感谢您的评论。实际上,我建议坚持使用LocalDate库