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 當前日期