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: 2019
Current Calendar's Day: 16
Current MINUTE: 0
Current 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 2019
Date 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 : 18
Time 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和格鲁吉亚日历。

翻译自: https://www.includehelp.com/scala/how-to-check-current-date-and-time-in-scala.aspx

scala 当前日期

scala 当前日期_如何在Scala中检查当前日期和时间?相关推荐

  1. java+script+当前日期_如何在JavaScript中获取当前日期?

    如何在JavaScript中获取当前日期? #1楼 您可以使用扩展了 Date对象的Date.js库,从而可以使用.today()方法. #2楼 如果您想对日期格式进行更多的粒度控制,我强烈建议您查看 ...

  2. python3获取当前日期_如何在python3中获取当前日期和时间? – Python3教程

    在本文中,你将学习如何用Python获取今天的日期和当前的日期和时间,我们还将使用strftime()方法以不同的格式格式化日期和时间. 获取当前日期的方法有很多,我们将使用datetime模块的da ...

  3. ubuntu修改ip地址后如何保存_如何在 Ubuntu 中检查你的 IP 地址 | Linux 中国

    不知道你的 IP 地址是什么?以下是在 Ubuntu 和其他 Linux 发行版中检查 IP 地址的几种方法.-- Sergiu 不知道你的 IP 地址是什么?以下是在 Ubuntu 和其他 Linu ...

  4. mysql计算秒_如何在MySQL中基于秒计算时间?

    让我们首先创建一个表-mysql> create table DemoTable ( Logouttime time ); 使用插入命令在表中插入一些记录-mysql> insert in ...

  5. java中如何检查字符串都是数字_如何在Java中检查字符串是否为数字?

    我们将检查字符串是否为数字-借助逻辑,我们将解决此问题,第一步,我们将使用一个名为str的字符串变量,并将任何值存储在其中. 在第二步中,我们将使用一个名为str_numeric的布尔变量,该变量存储 ...

  6. golang判断结构体为空_如何在Golang中检查结构是否为空?

    golang判断结构体为空 The size of an empty structure is zero in Golang. Here, empty structure means, there i ...

  7. 查看linux可用磁盘空间_如何在Linux中检查可用磁盘空间

    查看linux可用磁盘空间 跟踪磁盘利用率信息在系统管理员(和其他人员)的日常任务清单上. Linux有一些内置的实用程序可以帮助提供这些信息. df df命令代表"无磁盘",并显 ...

  8. java 检测目录下的文件_如何在Java中检查文件是目录还是文件

    java 检测目录下的文件 java.io.File class contains two methods using which we can find out if the file is a d ...

  9. vlc视频_如何在VLC中检查视频的比特率

    vlc视频 A video's bitrate is a key piece of information in determining the quality of said video. Even ...

最新文章

  1. CUDA 8混合精度编程
  2. 要不要跳槽,看完就懂了
  3. 配置管理小报111106:在wincvs中查找文件
  4. idea配置jfinal_intellij idea安装与配置(Java开发配置篇)
  5. 3、Oracle表空间管理
  6. 项目--properties--Builder;MyEclipse---project---clean---指定项目
  7. mysql分区表达式_怎么定义 mysql hash分区使用的用 户定义的表达式
  8. android 自动读取ecxel_android 读取excel表格数据,并存入数据库
  9. 系统学习NLP(十六)--DSSM
  10. 纪念硕士论文圆满答辩结束——20180614
  11. C# Cron表达式解析 .net 项目文件
  12. 【实用软件】picasa不能导入文件夹至其中 的问题解决
  13. Rendezvous: A Search Engine for Binary Code
  14. 美国会委员会建议禁止中国国企收购美国资产
  15. DELPHI 旧控件安装到 DELPHI11 新版环境的操作
  16. 智能管家项目总结(1)
  17. RK3588 camera2 支持4K录像
  18. oracle mysql sqlserver对比_Mysql、Oracle、SqlServer的JDBC连接实现和对比(提供驱动包)...
  19. kafka跨库同步mysql表_canal实时同步mysql表数据到Kafka
  20. 正则表达式30分钟入门

热门文章

  1. endnote国标_Citavi 与 Endnote 在 Word 插入引用,哪个更适合你?
  2. ovation系统服务器安装,Ovation系统介绍.ppt
  3. Angular之ngx-permissions安装入门
  4. C#判断回文字符串【C#】
  5. 【学习Android NDK开发】Type Signatures(类型签名)
  6. javaSE知识点汇总
  7. 蚂蚁金服亿级并发下的移动端到端网络接入架构解析
  8. return ,continue,break的用法与区别总结
  9. xpath选择当前结点的子节点
  10. JAVA 取得当前目录的路径/Servlet/class/文件路径/web路径/url地址