博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
scala 当前日期_如何在Scala中检查当前日期和时间?
阅读量:2531 次
发布时间:2019-05-11

本文共 8139 字,大约阅读时间需要 27 分钟。

scala 当前日期

Scala is a general-purpose programming language, which is majorly used for data manipulation. It has libraries and support for almost all different utilities that are important. One of the important things that are required while programming is real-time date and time status and Scala has a solution for that too. This tutorial on check current date and time in Scala walks you through the method to create Scala variables that hold date and time stamps.

Scala是通用编程语言,主要用于数据处理。 它具有库和对几乎所有重要的不同实用程序的支持。 编程时所需的重要内容之一是实时日期和时间状态 ,Scala也提供了解决方案。 本教程介绍了如何在Scala中检查当前日期和时间,并逐步引导您创建包含日期和时间戳的Scala变量。

使用日历类获取Scala中的当前日期和时间 (Getting current Date and Time in Scala using calendar class)

Getting current date and time in Scala is very simple and you can do this using the classical java 'calendar' class.

在Scala中获取当前日期和时间非常简单,您可以使用经典的java'calendar'类来实现

This class can be imported using the statement: "java.util.Calendar". The calendar class in java is an abstract class that is used to get the current date and time. The same class is used in Scala too, we can get the current date and time using it. There is a field in this class that stores the current date and time.

可以使用以下语句导入此类: “ java.util.Calendar” 。 java中的calendar类是一个抽象类,用于获取当前日期和时间 。 Scala中也使用了相同的类,我们可以使用它来获取当前日期和时间 。 此类中的一个字段用于存储当前日期和时间

Program to get time using getInstance() method

程序使用getInstance()方法获取时间

import java.util.Calendar;object MyClass {
def main(args: Array[String]) {
var dT = Calendar.getInstance() var currentMinute = dT.get(Calendar.MINUTE) var currentHour = dT.get(Calendar.HOUR_OF_DAY) if(currentHour > 12){
currentHour %= 12 println("Current time is "+currentHour+":"+currentMinute+" PM") } else{
println("Current time is "+currentHour+":"+currentMinute+" AM") } } }

Output

输出量

Current time is 7:50 PM

Code logic

代码逻辑

In this program, we have used the getInstance() method of the Calendar class. This method is used to find the date and time in java, the same method is used in Scala also, We have passed the variables calendar.MINUTE and calendar.HOUR_OF_DAY, and stored the result of the methods in variables currentMinute and currentHour respectively. The values returned from the functions are stored in the variables, The method gets (Calendar.HOUR_OF_DAY) returns the current hour in 24-hour format. To convert this into 12-hour format we will use a conditional statement that checks for AM or PM.

在此程序中,我们使用了Calendar类的getInstance()方法。 该方法用于在Java中查找日期和时间,Scala中也使用相同的方法,我们已传递了变量calendar.MINUTE和calendar.HOUR_OF_DAY ,并将方法的结果分别存储在变量currentMinute和currentHour中 。 从函数返回的值存储在变量中,方法get( Calendar.HOUR_OF_DAY )以24小时格式返回当前小时。 要将其转换为12小时格式,我们将使用条件语句检查AM或PM。

Program to get full date and time in Scala using Calendar Class

程序使用日历类获取Scala中的完整日期和时间

import java.util.Calendar;object MyClass {
def main(args: Array[String]) {
var dT = Calendar.getInstance() var currentHour = dT.getTime() println("Current data and time is "+currentHour) }}

Output

输出量

Current data and time is Tue Jul 16 19:54:59 UTC 2019

Code logic

代码逻辑

This code uses the getTime() method that returns the current date and time in the format day MM DD time UTC YYYY. This is also an inbuilt method of the class calendar.

此代码使用getTime()方法 ,该方法day MM DD time UTC YYYY的格式返回当前日期和时间 。 这也是类日历的内置方法。

You can also get the exact date and time of your current time based on UTC. Some methods help you get year, day, minute, the second also. You can get all this using the get() method of the Calendar class. Passing different parameters can get you different results. These are,

您还可以根据UTC获取当前时间的确切日期和时间 。 有些方法可以帮助您获得年,日,分钟,秒。 您可以使用Calendar类的get()方法获得所有这些信息。 传递不同的参数可以获得不同的结果。 这些是,

  • get(object.YEAR)

    get(object.YEAR)

  • get(object.DATE)

    get(object.DATE)

  • get(object.MINUTE)

    get(object.MINUTE)

  • get(object.SECOND)

    get(object.SECOND)

Program to find year, date, minute and second using get() method in Scala

程序在Scala中使用get()方法查找年,日期,分钟和秒

import java.util.Calendar;object MyClass {
def main(args: Array[String]) {
var dT = Calendar.getInstance(); println("Current Calendar's Year: " + dT.get(Calendar.YEAR)); println("Current Calendar's Day: " + dT.get(Calendar.DATE)); println("Current MINUTE: " + dT.get(Calendar.MINUTE)); println("Current SECOND: " + dT.get(Calendar.SECOND)); }}

Output

输出量

Current Calendar's Year: 2019Current Calendar's Day: 16Current MINUTE: 0Current SECOND: 5

使用java.time.localDate.Now获取日期 (Get Date using java.time.localDate.Now)

This java method is available in Scala, you can get the current date using this method.

Scala中提供了此java方法,您可以使用此方法获取当前日期

Program to get DATE in Scala

计划在Scala中获取DATE

import java.util.Calendar;object MyClass {
def main(args: Array[String]) {
println(java.time.LocalDate.now) }}

Output

输出量

2019-07-16

使用SimpleDateFormat类获取日期 (Get Date using SimpleDateFormat class)

This java class is also used in Scala to get current date. This class is used in Scala to get a date. This method is used with the scala calendar class to format the date into a specific form of our choice.

Scala中也使用此java类来获取当前日期 。 该类在Scala中用于获取日期。 此方法与scala日历类一起使用,以将日期格式化为我们选择的特定形式。

This class can be used by using the import statement: java.text.SimpleDateFormat. This imports these Java classes in Scala

可以通过使用import语句使用该类: java.text.SimpleDateFormat 。 这会将这些Java类导入Scala中

Program to get date using simpleDateFormat in Scala

程序在Scala中使用simpleDateFormat获取日期

import java.util.Calendar;import java.text.SimpleDateFormat;object MyClass {
def main(args: Array[String]) {
val form = new SimpleDateFormat("dd / MM / yy"); val c = Calendar.getInstance(); println("Present Date : " + c.getTime()); val formattedDate = form.format(c.getTime()); println("Date formatted : "+formattedDate); } }

Output

输出量

Present Date : Tue Jul 16 20:07:22 UTC 2019Date formatted : 16 / 07 / 19

使用SimpleDateFormat类获取时间 (Get Time using SimpleDateFormat class)

You can get the current time using the hour formatting over the calendar method variable.

您可以使用日历方法变量中的小时格式来获取当前时间

  • "HH" for getting hour in 24 hour format

    “ HH”以24小时制显示小时

  • "hh" for getting hour in 12 hour format

    “ hh”以12小时格式获取小时

  • "MM/mm" for getting Minutes

    “ MM / mm”用于获取分钟

  • "ss" for getting seconds

    “ ss”获得秒

  • "a" for getting am or pm

    “ a”表示上午或下午

Program to get current time using SimpleDateFormat class

程序使用SimpleDateFormat类获取当前时间

import java.util.Calendar;import java.text.SimpleDateFormat;object MyClass {
def main(args: Array[String]) {
val c = Calendar.getInstance(); val hr24 = new SimpleDateFormat("HH"); val formhr24 = hr24.format(c.getTime()); val hr12 = new SimpleDateFormat("hh"); val formhr12 = hr12.format(c.getTime()); val min = new SimpleDateFormat("mm"); val formmin = min.format(c.getTime()); val sec = new SimpleDateFormat("ss"); val formsec = sec.format(c.getTime()); val a_p = new SimpleDateFormat("a"); val forma_p = a_p.format(c.getTime()); println("Time in 24 hour format "+formhr24+" : "+formmin+" : "+formsec) println("Time in 24 hour format "+formhr12+" : "+formmin+" : "+formsec+" "+forma_p) }}

Output

输出量

Time in 24 hour format 20 : 12 : 18Time in 24 hour format 08 : 12 : 18 PM

The Java functions calendar() and SimpleDateFormat() are valid in Scala also. As in the above programs - you can see that the methods of these classes that are imported from Java are so powerful that they can manipulate all the date endtime functionalities that are inbuilt in the compiler. You can check ok dates in any format. Using this you can also see which calendar the compiler is using and the time that is being used by the compiler. Generally, the compiler uses UTC and Georgian calendar.

Java函数calendar()和SimpleDateFormat()在Scala中也有效。 就像上面的程序一样,您可以看到从Java导入的这些类的方法是如此强大,以至于它们可以操纵编译器中内置的所有日期结束时间功能。 您可以以任何格式检查确定日期。 使用此功能,您还可以查看编译器正在使用哪个日历以及编译器正在使用的时间。 通常,编译器使用UTC和格鲁吉亚日历。

翻译自:

scala 当前日期

转载地址:http://vytzd.baihongyu.com/

你可能感兴趣的文章
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第14节 高级篇幅之SpringBoot多环境配置_59、SpringBoot多环境配置介绍和项目实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_43、SpringBoot2.x异步任务实战(核心知识)...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_01课程简介
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第11节 Logback日志框架介绍和SpringBoot整合实战_45、SpringBoot2.x日志讲解和Logback配置实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_02技术选型
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_汇总
查看>>