Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert between Date, String and Calendar types

Shulou Source: shulou.com Published: 2022-06-02 10:23:56 10月04日 Update

How to convert between Date, String, Calendar types, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

Transforming 1.Calendar into String

/ / get the details of the current time, such as year, month, day, week,date, minutes, seconds, etc.

Calendar calendat = Calendar.getInstance ()

SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd")

String dateStr = sdf.format (calendar.getTime ())

Transforming 2.String into Calendar

String str= 2010-5-27

SimpleDateFormat sdf= new SimpleDateFormat ("yyyy-MM-dd")

Date date = sdf.parse (str)

Calendar calendar = Calendar.getInstance ()

Calendar.setTime (date)

Transforming 3.Date into String

SimpleDateFormat sdf= new SimpleDateFormat ("yyyy-MM-dd")

String dateStr=sdf.format (new Date ())

Transforming 4.String into Date

String str= 2010-5-27

SimpleDateFormat sdf= new SimpleDateFormat ("yyyy-MM-dd")

Date birthday = sdf.parse (str)

Transforming 5.Date into Calendar

Calendar calendar = Calendar.getInstance ()

Calendar.setTime (new java.util.Date ())

Transforming 6.Calendar into Date

Calendar calendar = Calendar.getInstance ()

Java.util.Date date = calendar.getTime ()

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Help between type clear content to this situation article novice time more knowledge industry information information channel problem demand channel such as year enter one Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Information MySQL Huawei NVidia