Java日期操作(包含Java8)

来源:互联网 发布:vfp编程题 编辑:程序博客网 时间:2024/04/27 23:20

java.time.MonthDay (Java8)

MonthDay represents the combination of the month and day. This class does not provide year. In the example I am showing some uses and working of MonthDay.
import java.time.MonthDay;public class MonthDayDemo {    public static void main(String[] args) {        MonthDay mday = MonthDay.now();        System.out.println(mday.getDayOfMonth());        System.out.println(mday.getMonth());        System.out.println(mday.atYear(2014));    }}

Find the output.Output
1SEPTEMBER2015-10-1

java.time.Month(Java 8)

Month is an enum and represents the complete months of the year. Find the uses of Month enum.
import java.time.Month;public class MonthDemo {    public static void main(String[] args) {        System.out.println(Month.MARCH);        System.out.println(Month.MARCH.getValue());        System.out.println(Month.of(3));        System.out.println(Month.valueOf("MARCH"));    }}
Find the output.Output
MARCH3MARCHMARCH

java.time.OffsetDateTime(Java 8)

OffsetDateTime represents all date and time fields. This class represents date and time with an offset. Find the uses of the OffsetDateTime.
import java.time.OffsetDateTime;public class OffsetDateTimeDemo {    public static void main(String[] args) {        OffsetDateTime offsetDT = OffsetDateTime.now();        System.out.println(offsetDT.getDayOfMonth());        System.out.println(offsetDT.getDayOfYear());        System.out.println(offsetDT.getDayOfWeek());        System.out.println(offsetDT.toLocalDate());    }}

Find the output.Output
11254THURSDAY2014-09-11

java.time.OffsetTime (Java 8)

OffsetTime represents time with an offset that can be viewed as hour-minute-second-offset. Find the use of OffsetTime.
import java.time.OffsetTime;public class OffsetTimeDemo {    public static void main(String[] args) {      OffsetTime offTime = OffsetTime.now();      System.out.println(offTime.getHour() +" hour");      System.out.println(offTime.getMinute() +" minute");      System.out.println(offTime.getSecond() +" second");    }}
Find the outputOutput
16 hour39 minute24 second




java.util.Date & Calendar

Java对日期进行加减运算,年份加减,月份加减。日期的操作用Calendar,表示用Date。
import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class DateTestUtil {        public static void main(String[] args) throws Exception {                SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");        String str="20110823";        Date dt=sdf.parse(str);        Calendar rightNow = Calendar.getInstance();        rightNow.setTime(dt);        rightNow.add(Calendar.YEAR,-1);//日期减1年        rightNow.add(Calendar.MONTH,3);//日期加3个月        rightNow.add(Calendar.DAY_OF_YEAR,10);//日期加10天        Date dt1=rightNow.getTime();        String reStr = sdf.format(dt1);        System.out.println(reStr);    }}//注:在Calendar对象的add方法中,第二个参数为正数表示“加”,负数表示“减”//获取系统当前时间(String类型)public static String getStringDateShort() {     Date currentTime = new Date();     SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");     String dateString = formatter.format(currentTime);     return dateString;    } 

Java 7 求对应的周数和星期

import java.text.DecimalFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Scanner;public class Main {public static void main(String[] args) throws ParseException {SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");String str = "2015-2-8";System.out.println("str="+str);Date date = dateFormatter.parse(str);dateFormatter.applyPattern("D");System.out.println("一年中的第几天:" + dateFormatter.format(date));dateFormatter.applyPattern("d");System.out.println("一个月中的第几天:" + dateFormatter.format(date));dateFormatter.applyPattern("w");System.out.println("一年中的第几周:" + dateFormatter.format(date));dateFormatter.applyPattern("W");System.out.println("一个月中的第几周:" + dateFormatter.format(date));dateFormatter.applyPattern("E");System.out.println("一个星期中的天数:" + dateFormatter.format(date));}}




0 0
原创粉丝点击