java 怎么获取当天日期,并将日期转化成数值?

Python07

java 怎么获取当天日期,并将日期转化成数值?,第1张

import java.text.SimpleDateFormatimport java.util.Date\x0d\x0apublic class Timedemo {\x0d\x0a public static void main(String[] args)\x0d\x0a {\x0d\x0a long time=System.currentTimeMillis()\x0d\x0a Date date=new Date(time)\x0d\x0a String mat="yyyy-MM-dd"\x0d\x0a String ma="yyyyMMdd"\x0d\x0a SimpleDateFormat format=new SimpleDateFormat(mat)\x0d\x0a SimpleDateFormat forma=new SimpleDateFormat(ma)\x0d\x0a String nowdate=format.format(date)\x0d\x0a String nwdate=forma.format(date)\x0d\x0a int x=Integer.parseInt(nwdate)\x0d\x0a System.out.println(nowdate)\x0d\x0a System.out.println(nwdate)\x0d\x0a System.out.println(x) \x0d\x0a }\x0d\x0a}\x0d\x0a\x0d\x0a经过测试满足以上条件 希望对你有帮助

    public static void main(String[] args) {

        //指定格式

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

        //将日期格式化为指定格式的字符串

        String cureDate = format.format(new Date())

        try {

            //将字符串按指定格式转化为日期

            Date date = format.parse(cureDate)

        } catch (ParseException e) {

            e.printStackTrace()

        }

    }

Date类型并没有格式,只有转换成String格式的时候让格式化显示。

new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")format(new Date())

Calendar calendar = Calendar.getInstance()

int year = Integer.parseInt(datetime.substring(0,4))

int month = Integer.parseInt(datetime.substring(5,7))

int date = Integer.parseInt(datetime.substring(8,10))

int hour = Integer.parseInt(datetime.substring(11,13))

int minute = Integer.parseInt(datetime.substring(14,16))

//int second = Integer.parseInt(datetime.substring(17,19))

if(calendar.get(Calendar.YEAR)>year){

int y = calendar.get(Calendar.YEAR)-year

扩展资料:

Date类可以在java.util包中找到,用一个long类型的值表示一个指定的时刻。它的一个有用的构造函数是Date(),创建一个表示创建时刻的对象。getTime()方法返回Date对象的long值。

import java.util.*

public class Now {

public static void main(String[] args) {

Date now = new Date()

long nowLong = now.getTime()

System.out.println("Value is " + nowLong)

参考资料来源:百度百科-java日期函数