Time in Java

Source: Internet
Author: User
Tags date1 dateformat

/**

* Watch API Document Learning: Many methods fail in date;

*

**/
Import Java.text.DateFormat;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;
Import Java.util.Date;
public class Datedemo {
public static void Main (string[] args) throws ParseException {
/**
* Calculate "2012/3/17" "2012-5-28" Interval days
* 1. The millisecond value gets subtracted
* 2. Get millisecond value, string--"date object--millisecond value
*/
GetDays ();
}
public static void Getseconds () {
DateFormat df = new SimpleDateFormat ("Yyyy-mm-dd");
Date date = df. Parse ("2017-06-25");
Date Date1=new date ();
Date date1 = Df.parse ("2017-06-24");
Calendar cal = Calendar.getinstance ();
Calendar cal1=calendar.getinstance ();
Cal.settime (date);
Cal1.settime (date1);
Long timestamp = Cal.gettimeinmillis ();
Long Timestamp1=cal1.gettimeinmillis ();
System.out.println (Timestamp1-timestamp);
}
public static void GetDays () throws parseexception{
String str_date1= "2012/3/17";
String str_date2= "2012-5-28";
date format string resolves to date object
1. customizing YYYY/MM/DD Style objects
DateFormat dateformat1=new SimpleDateFormat ("Yyyy/mm/dd");
2. Define a default style
DateFormat dateformat2=dateformat.getdateinstance (Dateformat.medium);
Check for error verification if the system defaults to English YY/MM/DD
Date Date=new date ();
String Str_date=dateformat2.format (date);
System.out.println (str_date);

3. Parsing a date format string
Date Date1=dateformat1.parse (str_date1);
Date Date2=dateformat2.parse (STR_DATE2);
4. Get millisecond values from date objects
Long Time1=date1.gettime ();
Long Time2=date2.gettime ();
5. Subtraction
Long Time=math.abs (TIME2-TIME1);
6. Milliseconds Turn day
int day= (int) (TIME/1000/60/60/24);
System.out.println (day);

}
public static void Cal () {
Demo Calendar
Calendar ca=calendar.getinstance ();
SYSTEM.OUT.PRINTLN (CA);
Calendar: Wrap time-related things in a map collection in a key-value pair
/*
*
* Java.util.gregoriancalendar[time=1497856585181,arefieldsset=true,
* Areallfieldsset=true,lenient=true,
* zone=sun.util.calendar.zoneinfo[id= "Asia/shanghai",
* Offset=28800000,dstsavings=0,usedaylight=false,transitions=19,lastrule=null],
* Firstdayofweek=1,minimaldaysinfirstweek=1,era=1,year=2017,month=5,week_of_year=25,
* week_of_month=4,day_of_month=19,day_of_year=170,day_of_week=2,
* Day_of_week_in_month=3,am_pm=1,hour=3,
* Hour_of_day=15,minute=16,second=25,millisecond=181,zone_offset=28800000,dst_offset=0]
*
*/
Define dates in a Calendar object
Ca.set (Calendar.year, 2015);
Ca.set (2012, 2, 2);
Date Offset
Ca.add (Calendar.year,-4);
Date offset is more ruthless.
Ca.add (Calendar.month, 7);
Showdate (CA);
How many days are there in any February 1? The day before March 1.
int year=2014;
Ca.set (year, 2,1);
Ca.add (Calendar.day_of_month,-1);
Showdate (CA);
Get yesterday's present moment.
Ca.add (Calendar.day_of_month,-1);
Showdate (CA);
}
public static void Showdate (Calendar CA) {
int Year=ca.get (calendar.year);
int Month=ca.get (calendar.month) +1;
int Day=ca.get (calendar.day_of_month);
int Week=ca.get (Calendar.day_of_week);
SYSTEM.OUT.PRINTLN (year+ "year" +month+ "month" +day+ "Day" +week);

}
public static void Date () {
Date date = new Date ();
SYSTEM.OUT.PRINTLN (date);
Format--Date to text parsing--Text to date
DateFormat provides a number of class methods to obtain the default date/time Formatter based on the default or given locale and a variety of formatting styles.
Formatting styles include full, LONG, MEDIUM, and short. More details and examples of using these styles are provided in the method description.
Obtaining an instance through the static factory method in the DateFormat class

DateFormat df = dateformat.getdateinstance ();

If the format is generally we can also add style
DF = dateformat.getdateinstance (Dateformat.short);
full:2017 Monday June 19
LONG:2017 year June 19
Medium:2017-6-19
Short:17-6-19
Convert to custom format xxxx/xx/xx can only customize objects. Use subclass//go to SimpleDateFormat class to find the total date and time pattern

DF = new SimpleDateFormat ("Yyyy/mm/dd hh:mm:ss");

Date + Time
DateFormat df1 = dateformat.getdatetimeinstance (Dateformat.full,
Dateformat.full);
String DATEFORMAT1 = Df1.format (date);
System.out.println (DATEFORMAT1);

Format a Date object using the Format method of the DateFormat

String DateFormat = Df.format (date);
System.out.println (DateFormat);
}
public static void Date2 () {
Millisecond value--"Date object constructor SetTime method
Long time = System.currenttimemillis ();
System.out.println (time);
Date date = new Date (time);
SYSTEM.OUT.PRINTLN (date);
DateFormat df = dateformat.getdateinstance (Dateformat.long);
String df_date = Df.format (date);
System.out.println (df_date);
Date Object--"millisecond value calculates the time difference (timestamp)
Long time1 = Date.gettime ();
System.out.println (TIME1);
}
public static void Date1 () throws ParseException {
Date date = new Date ();
SYSTEM.OUT.PRINTLN (date);//Background output system time but the format can not understand
Format Conversion xxxx-yy-mm
String str = "2016-09-07";
DateFormat df = dateformat.getdateinstance ();

Date date1 = Df.parse (str);
System.out.println (date1);
}
}

Time in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.