How to initialize a variable of date type in java?

前端 未结 5 2074
萌比男神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:27

    Here's the Javadoc in Oracle's website for the Date class: https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
    If you scroll down to "Constructor Summary," you'll see the different options for how a Date object can be instantiated. Like all objects in Java, you create a new one with the following:

    Date firstDate = new Date(ConstructorArgsHere);
    

    Now you have a bit of a choice. If you don't pass in any arguments, and just do this,

    Date firstDate = new Date();
    

    it will represent the exact date and time at which you called it. Here are some other constructors you may want to make use of:

    Date firstDate1 = new Date(int year, int month, int date);
    Date firstDate2 = new Date(int year, int month, int date, int hrs, int min);
    Date firstDate3 = new Date(int year, int month, int date, int hrs, int min, int sec);
    
    0 讨论(0)
  • 2021-02-20 03:29

    java.util.Date constructor with parameters like new Date(int year, int month, int date, int hrs, int min). is deprecated and preferably do not use it any more. Oracle docs prefers the way over java.util.Calendar. So you can set any date and instantiate Date object through the getTime() method.

    Calendar calendar = Calendar.getInstance();
    calendar.set(2018, 11, 31, 59, 59, 59);
    Date happyNewYearDate = calendar.getTime();
    

    Notice that month number starts from 0

    0 讨论(0)
  • 2021-02-20 03:36

    To parse a Date from a String you can choose which format you would like it to have. For example:

    public Date StringToDate(String s){
    
        Date result = null;
        try{
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            result  = dateFormat.parse(s);
        }
    
        catch(ParseException e){
            e.printStackTrace();
    
        }
        return result ;
    }
    

    If you would like to use this method now, you will have to use something like this

    Date date = StringToDate("2015-12-06 17:03:00");
    

    For more explanation you should check out http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

    0 讨论(0)
  • 2021-02-20 03:36

    tl;dr

    Use Instant, replacement for java.util.Date.

    Instant.now()  // Capture current moment as seen in UTC.
    

    If you must have a Date, convert.

    java.util.Date.from( Instant.now() ) 
    

    java.time

    The java.util.Date & .Calendar classes have been supplanted by the java.time framework built into Java 8 and later. The new classes are a tremendous improvement, inspired by the successful Joda-Time library.

    The java.time classes tend to use static factory methods rather than constructors for instantiating objects.

    To get the current moment in UTC time zone:

    Instant instant = Instant.now();
    

    To get the current moment in a particular time zone:

    ZoneId zoneId = ZoneId.of( "America/Montreal" );
    ZonedDateTime zdt = ZonedDateTime.now( zoneId );
    

    If you must have a java.util.Date for use with other classes not yet updated for the java.time types, convert from Instant.

    java.util.Date date = java.util.Date.from( zdt.toInstant() );
    


    About java.time

    The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

    To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

    The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

    You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

    Where to obtain the java.time classes?

    • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
    • Java SE 6 and Java SE 7
      • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • Later versions of Android bundle implementations of the java.time classes.
      • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

    The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
提交回复
热议问题