In the past few days, we have been doing time-based user authentication queries. Because in MySQL, the time field uses the bigint type, all of which are in milliseconds. To convert the millisecond value from the database to the date format, you need to use the functions of the MySQL database. You only need to call the functions in the SQL statement, but there is a problem with my business.
In MySQL, we often useFrom_unixtime (unix_timestamp, Format)Function to convert to the date format. The usage is as follows:
--------------------------------- The following is an excerpt from the webpage -----------------------------------------------
Returns a string indicating the time mark, formatted according to the format string. Format can contain the same modifier as the entries listed by the date_format () function.
Format the date value based on the format string.
The following modifiers can be used in a format string:
% M month name (January ...... December)
% W name of the Week (Sunday ...... Saturday)
% D indicates the date of the month with an English prefix (1st, 2nd, 3rd, and so on .)
% Y year, number, 4 digits
% Y year, number, 2 digits
% A abbreviated name of the Week (Sun ...... Sat)
% D number of days in the month (00 ...... 31)
% E number of days in the month (0 ...... 31)
% M month, number (01 ...... 12)
% C month, number (1 ...... 12)
% B abbreviated month name (Jan ...... Dec)
% J days in a year (001 ...... 366)
% H hour (00 ...... 23)
% K hour (0 ...... 23)
% H hour (01 ...... 12)
% I hour (01 ...... 12)
% L hour (1 ...... 12)
% I minute, number (00 ...... 59)
% R time, 12 hours (HH: mm: ss [AP] m)
% T time, 24 hours (HH: mm: SS)
% S seconds (00 ...... 59)
% S seconds (00 ...... 59)
% P am or PM
% W days in a week (0 = Sunday ...... 6 = Saturday)
% U Week (0 ...... 52). Sunday is the first day of the week.
% U Week (0 ...... 52) Monday is the first day of the week.
% A text "% ".
Example of Timestamp syntax interpretation using MySQL statements:
Select from_unixtime (1234567890, '% Y-% m-% d % H: % I: % s ')
------------------------------------- End of transcription -----------------------------------------------------
Since we can convert the millisecond in MySQL to the date format, we also need to convert the date format in the business logic to the millisecond value.
This is a function written by my senior student.
------------------------------------- The code is as follows -----------------------------------------------------
Public static long timestrtran1970seconds (string timestr) throws java. Text. parseexception {
Date Stat = NULL;
Stat = new simpledateformat ("yyyy-mm-dd hh: mm: SS"). parse (timestr );
If (stat! = NULL)
Return Stat. gettime ()/1000;
Else
Return 0l;
}
------------------------------------ Code end ------------------------------------------------------
Note that the two conversion time methods are based on 1970: 01: 01.