Java date and time processing/date,string,calendar conversion

Source: Internet
Author: User
Tags time and date

 Public classDemo01 {in//javaIntroduction to the Date class and calendar     Public Static voidMain (string[] args) {Longnow=System.currenttimemillis (); System.out.println ("Now=" +Now ); Date D1=NewDate (now); System.out.println ("D1=" +D1); Calendar C1=calendar.getinstance (); System.out.println ("C1=" +c1.gettime ()); }}
2. Basic operation of time and date

ImportJava.text.Format;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date; Public classDemo02_date_string_calendar { Public Static voidMain (string[] args)throwsparseexception {Date D1=NewDate (System.currenttimemillis ()); Calendar C1=calendar.getinstance ();//It's all about getting the current system time .//FormattingSimpleDateFormat sdf=NewSimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); System.out.println ("D1= before formatting" +d1.tostring ()); System.out.println ("D1= before formatting" +Sdf.format (D1)); //string and date conversionsString date= "2015-11-16"; SimpleDateFormat SDF1=NewSimpleDateFormat ("Yyyy-mm-dd"); Date D2=sdf1.parse (date);//use the parse function to convert a string type to a date type//you need to create an object to receive the converted objectSystem.out.println ("string converted to date,d2=" +D2); //Date converted to StringString s1=Sdf1.format (D2); System.out.println ("Date is converted to string type, s1=" +S1); }}
Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date;ImportJava.util.GregorianCalendar; Public classDemo03 { Public Static voidMain (string[] args)throwsParseException {//Set Birthday StringString birthday= "2015-05-30"; //Convert a string type to a dateSimpleDateFormat sdf=NewSimpleDateFormat ("Yyyy-mm-dd"); Date Date=sdf.parse (birthday); //to calculate a dateDate spec=preparebirthdayday (date); //Convert calculation results to string type outputString S2=sdf.format (spec);//Dateto StringSystem.out.println (birthday+ "2 weeks ahead of Saturday for:" +S2); }    /*** Pass in a date that calculates the date of week 6 of 2 weeks before the date * *@parambirthdaydate Incoming Date *@return2 weeks before the date of Saturday*/     Public StaticDate preparebirthdayday (date birthdaydate) {Calendar cal=NewGregorianCalendar ();//Parent class declaration Create subclass ObjectCal.settime (birthdaydate);//Receive incoming date dates converted to calendarCal.add (Calendar.week_of_month,-2);//the first week of the month 2 weeks in advanceCal.set (Calendar.day_of_week, calendar.saturday);//adjust to the current week of Week 6        returnCal.gettime ();//Calendar converted to date    }}

  

Java date and time processing/date,string,calendar conversion

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.