Five Questions of the Day - Java Interview Questions 220903

1. Is SimpleDateFormat thread-safe in a multi-threaded environment?

No, unfortunately, all implementations of DateFormat, including SimpleDateFormat, are not thread-safe, so you should not use them in multithreaded programs, except in external thread-safe environments, such as confining SimpleDateFormat to ThreadLocal. If you don't do this, you may get an incorrect result when parsing or formatting dates. Therefore, for all practices of date and time processing, I strongly recommend the joda-time library.

2. How to format a date in Java? Such as the form of ddMMyyyy?

In Java, you can use the SimpleDateFormat class or the joda-time library to format dates. The DateFormat class allows you to format dates using a variety of popular formats.

  • The current date is formatted with yyyy-MM-dd

      //Current local date, no time zone
        LocalDate date =;
        //Time and date formatting, the expression is the same as the well-known
        DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd");
        String dateStr = fmt.print(date);
  • Current date and time yyyy-MM–dd HH:mm:ss format

    //Current date time, no time zone
            LocalDateTime now =;
            //Date formatting, generic time expressions
            DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
            //2018-11-29 08:08:24
            String nowStr = fmt.print(now);
  • Parse datetime and use system timezone (example: 2019-10-28 10:23:12 )

            String dateStr = "2019-10-28 10:23:12";
            //Generate DateTimeFormatter object based on time expression
            DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
            DateTime dateTime = fmt.parseDateTime(dateStr);

3. In Java, how to display the time zone in the formatted date?

1. Add Z at the end of the format to indicate the time zone! For example: String timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS Z").format(new Date());

4. What is the difference between java.util .Date and java.sql.Date in Java?

  • java.util.Date : Usually used to get the current time or construction time.
  • java.sql.Date : is used for SQL statements, it contains only the date and no time part.

the difference:

  • Common ground:
    • Both getTime methods return the number of milliseconds, which can be constructed directly
  • Different points:
    • Java.sql. Date is used for SQL statements. It contains only dates but no time part. It is generally used when reading and writing databases. The parameters of setDate() of PreparedStatement and getDate() method of ResultSet are both java.sql.Date
    • java.util.Date is used in addition to SQL statements, usually daily date fields.
    • java.util.Date is the parent class of java.sql.Date, namely: inheritance relationship: java.lang.Object --> java.util.Date --> java.sql.Date

mutual conversion:

  1. java.sql.Date to java.util.Date

    java.sql.Date date = new java.sql.Date();
    java.util.Date d = new java.util.Date(date.getTime());
  2. java.util.Date to java.sql.Date

    java.util.Date utilDate = new java.util.Date();

    java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

    java.util.Date utilDate = new java.util.Date();

    java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

    java.sql.Time sTime = new java.sql.Time(utilDate.getTime());

    java.sql.Timestamp stp = new java.sql.Timestamp(utilDate.getTime());

  3. .Here all dates can be formatted by SimpleDateFormat

    SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    java.sql.Date sqlDate=java.sql.Date.valueOf("2005-12-12");
    utilDate=new java.util.Date(sqlDate.getTime());

5. How to calculate the difference between two dates in Java?

public static String getDatePoor(Date endDate, Date nowDate) {

    longnd = 1000* 24* 60* 60;

    longnh = 1000* 60* 60;

    longnm = 1000* 60;

    // long ns = 1000;

    // Get millisecond time difference of two times

    long diff = endDate.getTime() - nowDate.getTime();

    // Calculate how many days

    long day = diff / nd;

    // Calculate the difference in hours

    long hour = diff % nd / nh;

    // Calculate the difference in minutes

    long min = diff % nd % nh / nm;

    // Calculate the difference in seconds//output the result

    // long sec = diff % nd % nh % nm / ns;

    return day + "sky"+ hour + "Hour"+ min + "minute";

Source Link - Nuggets

Tags: Java Interview programming language

Posted by sford999 on Tue, 06 Sep 2022 22:05:48 +0530