java get milliseconds: System.currentTimeMillis ()
Java gets nanoseconds: System.nanoTime () (used to get very accurate time, recommended!)
java get the current date:
GregorianCalendar date_1 = (GregorianCalendar) Calendar.getInstance ();
java.sql.Date date_2 = new java.sql.Date (date_1.getTime (). getTime ());
-------------------------------------------------- -------------------------------------
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateTest {
public static void main (String [] args) throws ParseException {
Calendar now = Calendar.getInstance ();
System.out.println ("Year:" + now.get (Calendar.YEAR));
System.out.println ("Month:" + (now.get (Calendar.MONTH) + 1));
System.out.println ("Day:" + now.get (Calendar.DAY_OF_MONTH));
System.out.println ("When:" + now.get (Calendar.HOUR_OF_DAY));
System.out.println ("Point:" + now.get (Calendar.MINUTE));
System.out.println ("second:" + now.get (Calendar.SECOND));
System.out.println ("Current time in milliseconds:" + now.getTimeInMillis ());
System.out.println (now.getTime ());
Date d = new Date ();
System.out.println (d);
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss");
// SimpleDateFormat sdf = new SimpleDateFormat ("yyyy / MM / dd HH: mm: ss");
String dateNowStr = sdf.format (d);
System.out.println ("Formatted date:" + dateNowStr);
String str = "2012-08-20 17:52:07"; // It must be in the same format as defined above
Date today = sdf.parse (str);
System.out.println ("String converted to date:" + today);
}
}
Output result:
Year: 2012
Month: 8
Day: 20
Hours: 17
Points: 53
Second: 46
Current time in milliseconds: 1345456426312
Mon Aug 20 17:53:46 CST 2012
Mon Aug 20 17:53:46 CST 2012
Date after formatting: 2012-08-20 17:53:46
String to date: Mon Aug 20 17:52:07 CST 2012
-------------------------------------------------- -------------------------------------
import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class TimeTest {
// Used to control global changes in the previous week, this week, and next week
private int weeks = 0;
private int MaxDate; // Maximum number of days in January
private int MaxYear; // The maximum number of days in a year
public static void main (String [] args) {
TimeTest tt = new TimeTest ();
System.out.println ("Get the date of the day:" + tt.getNowTime ("yyyy-MM-dd"));
System.out.println ("Get this Monday's date:" + tt.getMondayOFWeek ());
System.out.println ("Get the date of this Sunday:" + tt.getCurrentWeekday ());
System.out.println ("Get the date of last Monday:" + tt.getPreviousWeekday ());
System.out.println ("Get the date of last Sunday:" + tt.getPreviousWeekSunday ());
System.out.println ("Get the next Monday date:" + tt.getNextMonday ());
System.out.println ("Get the next Sunday:" + tt.getNextSunday ());
System.out.println ("Get the date of the corresponding week Saturday:" + tt.getNowTime ("yyyy-MM-dd"));
System.out.println ("Get the date of the first day of the month:" + tt.getFirstDayOfMonth ());
System.out.println ("Get the date of the last day of the month:" + tt.getDefaultDay ());
System.out.println ("Get the date of the first day of the previous month:" + tt.getPreviousMonthFirst ());
System.out.println ("Get the date of the last day of the previous month:" + tt.getPreviousMonthEnd ());
System.out.println ("Get the date of the first day of the next month:" + tt.getNextMonthFirst ());
System.out.println ("Get the date of the last day of the next month:" + tt.getNextMonthEnd ());
System.out.println ("Get how many days this year:" + tt.getMaxYear ());
System.out.println ("Get the date of the first day of the year:" + tt.getCurrentYearFirst ());
System.out.println ("Get the date of the last day of the year:" + tt.getCurrentYearEnd ());
System.out.println ("Get the date of the first day of last year:" + tt.getPreviousYearFirst ());
System.out.println ("Get the date of the last day of last year:" + tt.getPreviousYearEnd ());
System.out.println ("Get the date of the first day of next year:" + tt.getNextYearFirst ());
System.out.println ("Get the date of the last day of next year:" + tt.getNextYearEnd ());
System.out.println ("Get the first day to the last day of the quarter:" + tt.getThisSeasonTime (11));
System.out.println ("Get the number of days between two dates 2011-12-1 ~ 2012-7.29:" + TimeTest.getTwoDay ("2012-7-29", "2011-12-1"));
}
/ **
* Get the number of days between two days
* /
public static String getTwoDay (String sj1, String sj2) {
SimpleDateFormat myFormatter = new SimpleDateFormat ("yyyy-MM-dd");
long day = 0;
try {
java.util.Date date = myFormatter.parse (sj1);
java.util.Date mydate = myFormatter.parse (sj2);
day = (date.getTime ()-mydate.getTime ()) / (24 * 60 * 60 * 1000);
} catch (Exception e) {
return "";
}
return day + "";
}
/ **
* According to a date, return a string that is the day of the week
* /
public static String getWeek (String sdate) {
// convert to time
Date date = TimeTest.strToDate (sdate);
Calendar c = Calendar.getInstance ();
c.setTime (date);
// int hour = c.get (Calendar.DAY_OF_WEEK);
// The day of the week is stored in hour, the range is 1 ~ 7
// 1 = Sunday 7 = Saturday, other analogies
return new SimpleDateFormat ("EEEE"). format (c.getTime ());
}
/ **
* Convert short time format string to time yyyy-MM-dd
* /
public static Date strToDate (String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
ParsePosition pos = new ParsePosition (0);
Date strtodate = formatter.parse (strDate, pos);
return strtodate;
}
/ **
* Number of days between two times
* /
public static long getDays (String date1, String date2) {
if (date1 == null || date1.equals (""))
return 0;
if (date2 == null || date2.equals (""))
return 0;
// Convert to standard time
SimpleDateFormat myFormatter = new SimpleDateFormat ("yyyy-MM-dd");
java.util.Date date = null;
java.util.Date mydate = null;
try {
date = myFormatter.parse (date1);
mydate = myFormatter.parse (date2);
} catch (Exception e) {
}
long day = (date.getTime ()-mydate.getTime ()) / (24 * 60 * 60 * 1000);
return day;
}
// Calculate the last day of the month and return the string
public String getDefaultDay () {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance ();
lastDate.set (Calendar.DATE, 1); // Set to the 1st of the current month
lastDate.add (Calendar.MONTH, 1); // Add a month to become the 1st of the next month
lastDate.add (Calendar.DATE, -1); // Subtract one day to become the last day of the month
str = sdf.format (lastDate.getTime ());
return str;
}
// The first day of last month
public String getPreviousMonthFirst () {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance ();
lastDate.set (Calendar.DATE, 1); // Set to the 1st of the current month
lastDate.add (Calendar.MONTH, -1); // Subtract one month, become the 1st of next month
//lastDate.add(Calendar.DATE,-1);//Subtract one day to become the last day of the month
str = sdf.format (lastDate.getTime ());
return str;
}
// Get the first day of the month
public String getFirstDayOfMonth () {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance ();
lastDate.set (Calendar.DATE, 1); // Set to the 1st of the current month
str = sdf.format (lastDate.getTime ());
return str;
}
// Get the date of this Sunday
public String getCurrentWeekday () {
weeks = 0;
int mondayPlus = this.getMondayPlus ();
GregorianCalendar currentDate = new GregorianCalendar ();
currentDate.add (GregorianCalendar.DATE, mondayPlus + 6);
Date monday = currentDate.getTime ();
DateFormat df = DateFormat.getDateInstance ();
String preMonday = df.format (monday);
return preMonday;
}
// Get the time of day
public String getNowTime (String dateformat) {
Date now = new Date ();
SimpleDateFormat dateFormat = new SimpleDateFormat (dateformat); // You can easily modify the date format
String hehe = dateFormat.format (now);
return hehe;
}
// Get the number of days between the current date and this Sunday
private int getMondayPlus () {
Calendar cd = Calendar.getInstance ();
// Get today is the day of the week, Sunday is the first day, Tuesday is the second day ...
int dayOfWeek = cd.get (Calendar.DAY_OF_WEEK) -1; // Because Chinese Monday is used as the first day, here is reduced by 1
if (dayOfWeek == 1) {
return 0;
} else {
return 1-dayOfWeek;
}
}
// Get the date of this Monday
public String getMondayOFWeek () {
weeks = 0;
int mondayPlus = this.getMondayPlus ();
GregorianCalendar currentDate = new GregorianCalendar ();
currentDate.add (GregorianCalendar.DATE, mondayPlus);
Date monday = currentDate.getTime ();
DateFormat df = DateFormat.getDateInstance ();
String preMonday = df.format (monday);
return preMonday;
}
// Get the date of Saturday of the corresponding week
public String getSaturday () {
int mondayPlus = this.getMondayPlus ();
GregorianCalendar currentDate = new GregorianCalendar ();
currentDate.add (GregorianCalendar.DATE, mondayPlus + 7 * weeks + 6);
Date monday = currentDate.getTime ();
DateFormat df = DateFormat.getDateInstance ();
String preMonday = df.format (monday);
return preMonday;
}
// get the date of last Sunday
public String getPreviousWeekSunday () {
weeks = 0;
weeks--;
int mondayPlus = this.getMondayPlus ();
GregorianCalendar currentDate = new GregorianCalendar ();
currentDate.add (GregorianCalendar.DATE, mondayPlus + weeks);
Date monday = currentDate.getTime ();
DateFormat df = DateFormat.getDateInstance ();
String preMonday = df.format (monday);
return preMonday;
}
// get the date of last Monday
public String getPreviousWeekday () {
weeks--;
int mondayPlus = this.getMondayPlus ();
GregorianCalendar currentDate = new GregorianCalendar ();
currentDate.add (GregorianCalendar.DATE, mondayPlus + 7 * weeks);
Date monday = currentDate.getTime ();
DateFormat df = DateFormat.getDateInstance ();
String preMonday = df.format (monday);
return preMonday;
}
// get the date of next Monday
public String getNextMonday () {
weeks ++;
int mondayPlus = this.getMondayPlus ();
GregorianCalendar currentDate = new GregorianCalendar ();
currentDate.add (GregorianCalendar.DATE, mondayPlus + 7);
Date monday = currentDate.getTime ();
DateFormat df = DateFormat.getDateInstance ();
String preMonday = df.format (monday);
return preMonday;
}
// get the date of next Sunday
public String getNextSunday () {
int mondayPlus = this.getMondayPlus ();
GregorianCalendar currentDate = new GregorianCalendar ();
currentDate.add (GregorianCalendar.DATE, mondayPlus + 7 + 6);
Date monday = currentDate.getTime ();
DateFormat df = DateFormat.getDateInstance ();
String preMonday = df.format (monday);
return preMonday;
}
private int getMonthPlus () {
Calendar cd = Calendar.getInstance ();
int monthOfNumber = cd.get (Calendar.DAY_OF_MONTH);
cd.set (Calendar.DATE, 1); // Set the date to the first day of the month
cd.roll (Calendar.DATE, -1); // The date rolls back one day, which is the last day
MaxDate = cd.get (Calendar.DATE);
if (monthOfNumber == 1) {
return -MaxDate;
} else {
return 1-monthOfNumber;
}
}
// Get the date of the last day of the previous month
public String getPreviousMonthEnd () {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance ();
lastDate.add (Calendar.MONTH, -1); // minus one month
lastDate.set (Calendar.DATE, 1); // Set the date to the first day of the month
lastDate.roll (Calendar.DATE, -1); // The date rolls back one day, which is the last day of the month
str = sdf.format (lastDate.getTime ());
return str;
}
// Get the date of the first day of the next month
public String getNextMonthFirst () {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance ();
lastDate.add (Calendar.MONTH, 1); // minus one month
lastDate.set (Calendar.DATE, 1); // Set the date to the first day of the month
str = sdf.format (lastDate.getTime ());
return str;
}
// Get the date of the last day of the next month
public String getNextMonthEnd () {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance ();
lastDate.add (Calendar.MONTH, 1); // Add one month
lastDate.set (Calendar.DATE, 1); // Set the date to the first day of the month
lastDate.roll (Calendar.DATE, -1); // The date rolls back one day, which is the last day of the month
str = sdf.format (lastDate.getTime ());
return str;
}
// Get the date of the last day of next year
public String getNextYearEnd () {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance ();
lastDate.add (Calendar.YEAR, 1); // Add a year
lastDate.set (Calendar.DAY_OF_YEAR, 1);
lastDate.roll (Calendar.DAY_OF_YEAR, -1);
str = sdf.format (lastDate.getTime ());
return str;
}
// Get the date of the first day of next year
public String getNextYearFirst () {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance ();
lastDate.add (Calendar.YEAR, 1); // Add a year
lastDate.set (Calendar.DAY_OF_YEAR, 1);
str = sdf.format (lastDate.getTime ());
return str;
}
// Get how many days this year
private int getMaxYear () {
Calendar cd = Calendar.getInstance ();
cd.set (Calendar.DAY_OF_YEAR, 1); // Set the date to the first day of the year
cd.roll (Calendar.DAY_OF_YEAR, -1); // Roll the date back one day.
int MaxYear = cd.get (Calendar.DAY_OF_YEAR);
return MaxYear;
}
private int getYearPlus () {
Calendar cd = Calendar.getInstance ();
int yearOfNumber = cd.get (Calendar.DAY_OF_YEAR); // Get day is the day of the year
cd.set (Calendar.DAY_OF_YEAR, 1); // Set the date to the first day of the year
cd.roll (Calendar.DAY_OF_YEAR, -1); // Roll the date back one day.
int MaxYear = cd.get (Calendar.DAY_OF_YEAR);
if (yearOfNumber == 1) {
return -MaxYear;
} else {
return 1-yearOfNumber;
}
}
// Get the date of the first day of the year
public String getCurrentYearFirst () {
int yearPlus = this.getYearPlus ();
GregorianCalendar currentDate = new GregorianCalendar ();
currentDate.add (GregorianCalendar.DATE, yearPlus);
Date yearDay = currentDate.getTime ();
DateFormat df = DateFormat.getDateInstance ();
String preYearDay = df.format (yearDay);
return preYearDay;
}
// Get the date of the last day of the year *
public String getCurrentYearEnd () {
Date date = new Date ();
SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy"); // You can easily modify the date format
String years = dateFormat.format (date);
return years + "-12-31";
}
// Get the date of the first day of the previous year *
public String getPreviousYearFirst () {
Date date = new Date ();
SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy"); // You can easily modify the date format
String years = dateFormat.format (date); int years_value = Integer.parseInt (years);
years_value--;
return years_value + "-1-1";
}
// Get the date of the last day of the previous year
public String getPreviousYearEnd () {
weeks--;
int yearPlus = this.getYearPlus ();
GregorianCalendar currentDate = new GregorianCalendar ();
currentDate.add (GregorianCalendar.DATE, yearPlus + MaxYear * weeks + (MaxYear-1));
Date yearDay = currentDate.getTime ();
DateFormat df = DateFormat.getDateInstance ();
String preYearDay = df.format (yearDay);
getThisSeasonTime (11);
return preYearDay;
}
// Get this quarter
public String getThisSeasonTime (int month) {
int array [] [] = {{1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}};
int season = 1;
if (month> = 1 && month <= 3) {
season = 1;
}
if (month> = 4 && month <= 6) {
season = 2;
}
if (month> = 7 && month <= 9) {
season = 3;
}
if (month> = 10 && month <= 12) {
season = 4;
}
int start_month = array [season-1] [0];
int end_month = array [season-1] [2];
Date date = new Date ();
SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy"); // You can easily modify the date format
String years = dateFormat.format (date);
int years_value = Integer.parseInt (years);
int start_days = 1; //years+"-"+String.valueOf(start_month)+"-1 "; // getLastDayOfMonth (years_value, start_month);
int end_days = getLastDayOfMonth (years_value, end_month);
String seasonDate = years_value + "-" + start_month + "-" + start_days + ";" + years_value + "-" + end_month + "-" + end_days;
return seasonDate;
}
/ **
* Get the last day of a year and month
* @param year
* @param month month
* @return last day
* /
private int getLastDayOfMonth (int year, int month) {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
|| month == 10 || month == 12) {
return 31;
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
}
if (month == 2) {
if (isLeapYear (year)) {
return 29;
} else {
return 28;
}
}
return 0;
}
/ **
* Whether it is a leap year
* @param year
* @return
* /
public boolean isLeapYear (int year) {
return (year% 4 == 0 && year% 100! = 0) || (year% 400 == 0);
}
}