FranzWagner
FranzWagner

Reputation: 11

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

Upvotes: 0

Views: 4803

Answers (3)

Dushyant Tankariya
Dushyant Tankariya

Reputation: 1454

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));

    }
}

Upvotes: 0

Jens
Jens

Reputation: 69470

DateTimeFornatter is in package java.time.format.

So you have to add

import java.time.format.DateTimeFormatter

Upvotes: 0

Mladen Savić
Mladen Savić

Reputation: 472

You need to import DateTimeFormatter class so you can use it.

Upvotes: 1

Related Questions