scala 當前日期_如何在Scala中檢查當前日期和時間?

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541612.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541612.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541612.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

計算機科學考試大綱,計算機科學與技術考試大綱.doc

計算機科學與技術考試大綱計算機科學與技術專業本專業的專業課程考試為“計算機軟件基礎”和“計算機硬件基礎”兩門課程的組合試卷,卷面總分200分,時間150分鐘,考試方式為筆試。考試可攜帶計數器,但禁止攜帶文曲星、商務通等帶有…

eclipse中項目內存溢出問題

2019獨角獸企業重金招聘Python工程師標準>>> SpringBoot項目熱啟動Perm區內存溢出。 Failed to instantiate [org.springframework.orm.jpa.JpaVendorAdapter]: Factory method jpaVendorAdapter threw exception; nested exception is java.lang.OutOfMemoryErro…

云盾idaas登陸_移動端掃碼登錄IDaaS平臺

{"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],"search_count":[{"count_phone":10,"count":10}]},"card":[{"des":"刷臉門禁通行系統前端接入人臉AI賦能的人臉…

express rest_Express / Node中用于REST API的郵遞員工具

express restWhen dealing with routes (like in express), we may use any of the REST verbs and at times, the browser is limited to facilitate testing the routes/REST API. 在處理路由時(如快速表達),我們可以使用任何REST動詞,有時瀏覽器會受到…

我們在使用計算機時,不能做什么?,11秋季學期計算機應用技術基礎學習周期_01任務-在線作業[1]1...

1.選購顯示器應優先考慮顯示器的( B )性能指標選A.顯示器的防輻射指標B. 顯示器的帶寬C. 顯示器的刷新率D. 顯示器的尺寸2. 一個聲音文件采用雙聲道8位采樣精度、22K采樣頻率錄音,它的大小是5M,如果采用單聲道16位采樣精度、44K采樣頻率錄音,…

按一個按鈕會隨機死人_《饑荒》那些年坑爹的隨機地圖,最后一個簡直笑死人...

饑荒是一款隨機性很強的游戲,這也是饑荒這款游戲相當耐玩的主要原因。別的不說小編敢保證隨機開圖的話你絕對找不到兩張一模一樣的地圖。地圖上的資源也會大不相同。不管是稀有的各種奇遇還是基礎資源的刷新數量都是完全不同的。當然對于一些基礎資源玩家們并沒有多…

介詞at_介詞邏輯| 離散數學

介詞at介詞或陳述 (Preposition or Statement) A preposition is a definition sentence which is true or false but not both. 介詞是一個定義語句,它是對還是錯,但不能同時包含兩者。 For example: The following 8 sentences, 例如:以下…

職稱計算機提前考試試卷,職稱計算機考試多項選擇考試卷模擬考^試題

《職稱計算機考試多項選擇考試卷模擬考^試題》由會員分享,可在線閱讀,更多相關《職稱計算機考試多項選擇考試卷模擬考^試題(8頁珍藏版)》請在人人文庫網上搜索。1、姓名:________________ 班級:________________ 學號:…

形象易懂講解算法I——小波變換

https://zhuanlan.zhihu.com/p/22450818?referdong5 最早發于回答:能不能通俗的講解下傅立葉分析和小波分析之間的關系? - 咚懂咚懂咚的回答現收入專欄。從傅里葉變換到小波變換,并不是一個完全抽象的東西,可以講得很形象。小波變…

r語言安裝ipsolve_R語言矩陣操作之矩陣運算

1.轉置運算對于矩陣A,函數t(A)表示矩陣A的轉置,如:> Amatrix(1:6,nrow2);> A;[,1] [,2] [,3][1,] 1 3 5[2,] 2 4 6> t(A);[,1] [,2][1,] 1 2[2,] 3 4[3,] 5 62.求方陣的行列式函數det()是求矩陣…

使用Linux命令行歸檔文件

存檔文件 (Archiving Files) As we already understand what Compression (Compression techniques in Linux) is? We shall learn about Archives. We prefer compression as it is convenient to send file compressed through a network but sometimes it is not a smart w…

http緩存機制之304狀態碼

在網上看到一篇關于解釋瀏覽器緩存更新機制304狀態碼的文章,里面說如果請求頭中的If-Modified-Since字段和If-None-Match字段的值分別和響應頭中的Last-Modified字段和Etag字段值一致,服務器就會返回304狀態碼(無響應體),瀏覽器就從本地讀取緩…

東北大學 計算機技術導師,報考東北大學 計算機技術 329分 求調劑相關專業

自薦類型:碩士自薦報考院校:東北大學報考專業:(專業碩士)計算機技術[085211]本科院校:沈陽工程學院本科專業:計算機科學與技術初試成績:總分:329政治:69 英語:71 …

c語言i++和++i程序_使用C ++程序修改鏈接列表的內容

c語言i和i程序Problem statement: 問題陳述: Given a linked list, you modified that linked list in such a way that the elements of the first half of that linked list are the difference of the first node to the last node and next node is the differ…

原生js設置div隱藏或者顯示_10種JS控制DIV的顯示隱藏代碼

div隱藏與顯示#menus {background-color: #c4cff0;}function Layer_HideOrShow(cur_div){ var currentdocument.getElementById(cur_div);if(current.style.visibility"hidden"){current.style.visibility "visible";}else{current.style.visibility "…

計算機工作對身體有害嗎,在電腦前長時間工作會對身體有害處嗎?

病情分析:目前,電腦對人體生理和心理方面的負面影響已日益受到人們的重視.為此科學使用電腦,減少電腦和網絡的危害是十分必要的.指導意見:一是要增強自我保健意識工作間隙注意適當休息,一般來說,電腦操作人員在連續工作1小時后應該休息10分鐘左右.并且最…

Java LinkedList getFirst()方法與示例

LinkedList getFirst()方法 (LinkedList getFirst() method) This method is available in package java.util.LinkedList. 軟件包java.util.LinkedList中提供了此方法。 This method is used to return the first or initial or beginning element of the linked list. 此方法…

C++第15周(春)項目2 - 用文件保存的學生名單

課程首頁在:http://blog.csdn.net/sxhelijian/article/details/11890759。內有完整教學方案及資源鏈接本程序中須要的相關文件。請到http://pan.baidu.com/s/1qW59HTi下載。【項目2-用文件保存的學生名單】  文件score.dat中保存的是若干名學生的姓名和C課、高數和…

計算機選配 注意事項,選擇鼠標注意事項有哪些

選擇鼠標注意事項有哪些每臺電腦旁邊都有了一個忠實的伴侶,那就是“Mouse”--鼠標。選擇鼠標最重要的一點就是質量,無論它的功能有多強大、外形多漂亮,如果質量不好那么一切都不用考慮了。那么,選擇鼠標注意事項有哪些?筆記本鼠標…

js 驗證護照_護照本地策略第2部分| Node.js

js 驗證護照In my last article (Passport local strategy section 1 | Node.js), we started the implementation of the passport-local authentication strategy. We also looked at the various requirements to get started with the login form. In this article, we wil…