Common uses of Java date classes
Date related classes
1.Date class
Contains a Date class in the standard Java class library, its object represents a specific moment, accurate to milliseconds. When placing an order in the online mall and reviewing the reimbursement form, you need to obtain the current time, which can be accomplished through the Date class.
Example: Use of Date class
package li.normalclass.date; import java.util.Date; public class TestDate { public static void main(String[] args) { //获取当前的时间 格式为 yyyyMMddhhmmss Date date = new Date();//相当于new Date(System.currentTimeMillis()) //操作当前的时间 System.out.println(date.toString());//Sat Aug 06 19:15:28 CST 2022 System.out.println(date.toLocaleString());//2022-8-6 19:16:06 System.out.println(System.currentTimeMillis());//计算从1970年1月1日 0:00:00到目前为止的毫秒数 System.out.println(date.getYear());//122 =2022-1900 System.out.println(date.getMonth());//7 0-11 现在是八月 System.out.println(date.getDate());//6 日 System.out.println(date.getDay());//6 当前为星期六 注:星期日为0 System.out.println(date.getHours());//19 当前为19点 System.out.println(date.getMinutes());//26 当前为26分 System.out.println(date.getSeconds());//16 当前为16秒 System.out.println(date.getTime());//1659785176358 计算从1970年1月1日 0:00:00到目前为止的毫秒数 //获取当前的时间 格式为 yyyyMMdd java.sql.Date sdate = new java.sql.Date(System.currentTimeMillis()); System.out.println(sdate.toString());//2022-08-06 java.sql.Date sdate2 = java.sql.Date.valueOf("1896-9-10"); System.out.println(sdate2.toString());//1896-09-10 } }
Looking at the API documentation, you can see that many methods in the Date class are obsolete. Date before JDK1.1 included operations such as date operations and converting strings into objects. After JDK1.1, date operation classes generally use the Calendar class, and string conversion uses the DateFormat class.
2.DateFormat class
Format: format
DateFormat is an abstract class, which is generally implemented using its subclass SimpleDateFormat class. The main function is to convert the time object into a string in a specified format. On the contrary, it is to convert the string in the specified format into a time object.
String----->Date
Date----->String
Example:
package li.normalclass.date; import java.text.*; import java.util.Date; /** * 主要操作: * DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//指定识别的格式 * * Date date = sdf.parse(strdate);//将字符串转换成日期 * * String strdate2 = sdf.format(date);//将日期转换成字符串 */ public class TestDateFormat { public static void main(String[] args) throws ParseException { String strdate = "1999-12-23 12:12:12";//字符串 //String---->Date //DateFormat是抽象类,要实例化只能引用它的子类SimpleDateFormat DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//指定识别的格式 Date date = sdf.parse(strdate);//将字符串转换成日期 String strdate2 = sdf.format(date);//将日期转换成字符串 System.out.println(strdate2); } }
3.Calendar class
Calendar: Calendar
Example:
package li.normalclass.date; import java.util.Calendar; import java.util.GregorianCalendar; public class TestCalendar { public static void main(String[] args) { //获取当前的时间 Calendar cal = new GregorianCalendar(); // 输出当前的时间 System.out.println(cal); //java.util.GregorianCalendar[time=1659791839017,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=31,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,MONTH=7,WEEK_OF_YEAR=32,WEEK_OF_MONTH=1,DAY_OF_MONTH=6,DAY_OF_YEAR=218,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=17,SECOND=19,MILLISECOND=17,ZONE_OFFSET=28800000,DST_OFFSET=0] System.out.println(cal.get(Calendar.YEAR));//2022 System.out.println(cal.get(Calendar.MONTH));//7 0~11 7代表8月 System.out.println(cal.get(Calendar.DATE));//6 代表6号 System.out.println(cal.get(Calendar.DAY_OF_WEEK));//7 代表周六 从周日为1开始计算一周 //改变时间 cal.set(Calendar.DATE,1);//直接指定日期 1号 cal.set(Calendar.MONTH,1);//直接指定月数 2月 cal.add(Calendar.DATE,2);//在设置的日期上再加上两天 System.out.println(cal.get(Calendar.YEAR));//2022 -- 22年 System.out.println(cal.get(Calendar.MONTH));//1 -- 2月 System.out.println(cal.get(Calendar.DATE));//3 -- 3号 System.out.println(cal.get(Calendar.DAY_OF_WEEK));//5 -- 周四 System.out.println(cal.getActualMaximum(Calendar.DATE));//28 -- 指定月一共有多少天 } }
The above is the detailed content of Common uses of Java date classes. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.
