简体   繁体   中英

How to use date time format

I am using date time format, to print date time in a specific pattern, but my compiler is throwing an error.

import java.util.Date; 
import java.time.*;
import java.text.SimpleDateFormat;  

public class fewdays{

    public static void main(String[] args){

        LocalDate today = LocalDate.now() ;
        LocalDate then = today.minusDays( 2 ) ;
        LocalTime time_ago = LocalTime.now();

        LocalDateTime dt = LocalDateTime.of(then, time_ago);

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        System.out.println(dtf.format(dt));

    }
}

Now the compiler shows this error message :

 error: cannot find symbol
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
                                    ^
 symbol:   variable DateTimeFormatter
 location: class fewdays

您需要导入DateTimeFormatter类,以便可以使用它。

DateTimeFornatter is in package java.time.format .

So you have to add

import java.time.format.DateTimeFormatter

I've Read your question and find out that it is the common import package error. So What you need to do here is Import java.time.format.DateTimeFormatter package to your program and See It will work.

I've already Correct your program and pasted below.

import java.util.Date; 
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.text.SimpleDateFormat;  

public class fewdays{

    public static void main(String[] args){

        LocalDate today = LocalDate.now() ;
        LocalDate then = today.minusDays( 2 ) ;
        LocalTime time_ago = LocalTime.now();

        LocalDateTime dt = LocalDateTime.of(then, time_ago);

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        System.out.println(dtf.format(dt));

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM