Java gets the year, month, day, hour, minute, and second of a specified date

learning target:

  • 1. Use java.util.Date in java to get the year of a specified date
  • 2. Use java.util.Date in java to get the month of a specified date
  • 3. Use java.util.Date in java to get the day of the specified date
  • 4. Use java.util.Date in java to get the time of a specified date
  • 5. Use java.util.Date in java to get the minutes of a specified date
  • 6. Use java.util.Date in java to get the seconds of a specified date

Learning Content:

内容如下所示:

1. Import java package

  import java.util.Date;

2. Use java.util.Date in java to get the year of a specified date

Date date=new Date();
String year=String.format("%tY", date);

3. Use java.util.Date in java to get the month of a specified date

Date date=new Date();
String mon=String .format("%tm", date);

4. Use java.util.Date in java to get the day of a specified date

Date date=new Date();
String day=String .format("%td", date);

5. Use java.util.Date in java to get the time of a specified date

 Date date=new Date();
 String h=String .format("%tH", date);

6. Use java.util.Date in java to get the minutes of a specified date

 Date date=new Date();
 String m=String .format("%tM", date);

7. Use java.util.Date in java to get the seconds of a specified date

     Date date=new Date();
     String s=String .format("%tS", date);

Summarize:

知识小结:

  • 1. The String.format() method is a method of formatting strings
  • 2. Output string: a placeholder "%s" represents the string itself to be output. (Define data type as string)
  • 3. Output integer: There is only one placeholder "%d", which represents the integer to be output. (Define data type as int)
  • 4. Output floating point numbers: There is only one placeholder "%.2f", which represents the floating point number to be output, where ".2" represents the number of decimal places to be output, that is, 2 decimal places are retained. (Define the data type as double)
  • 5. Output date: The placeholder "%tF" represents the date to be output. "%tF" means that the output date format is "year-month-day", so the format string is "%tF" and the parameter list is date. (Define the data type as Date)
  • 6. Output time: The placeholder "%tT" represents the time to be output. "%tT" indicates that the output time format is "hour:minute:second". The parameter list is date. (Define the data type as Date)
  • 7. Output bandwidth rate: a placeholder "%.2f" and a text "Mbps", which together represent the bandwidth rate to be output. (Define the data type as double)
  • 8. Output currency amount: a placeholder " %.2f", which represents the currency amount to be output, where ""Indicates the currency symbol, ".2" indicates the number of decimal places to be output, that is, retaining 2 decimal places. Therefore, the format string is "$%.2f" and the parameter list is amount. (The defined data type is double)
  • 9. "%e" outputs floating point numbers represented in scientific notation
  • 10. Use "%o" to output an integer represented in octal
  • 11. "%x" outputs integers represented in hexadecimal, etc.

Guess you like

Origin blog.csdn.net/YHLSunshine/article/details/132665806