How to initialize a variable of date type in java?

前端 未结 5 2148
萌比男神i
萌比男神i 2021-02-20 03:04
import java.util.Date;
Date firstDate; 

I don\'t know how to intialize the firstDate for example for String you say

String line1=\"Fir         


        
5条回答
  •  孤独总比滥情好
    2021-02-20 03:48

    To initialize to current date, you could do something like:

    Date firstDate = new Date();
    

    To get it from String, you could use SimpleDateFormat like:

    String dateInString = "10-Jan-2016";
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
    try {
        Date date = formatter.parse(dateInString);
        System.out.println(date);
        System.out.println(formatter.format(date));
    } catch (ParseException e) {
        //handle exception if date is not in "dd-MMM-yyyy" format
    }
    

提交回复
热议问题