关于jdk1.8时间处理的⽅法(实⽤)
package avg.position.zhangdi;
ParseException;
SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import poral.ChronoField;
import poral.ChronoUnit;
import poral.TemporalAccessor;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class DateTimeFormat {
public static void main(String[] args) throws ParseException {
/
*java.time包内,有⼏个⽐较重要的组件,Instant(时间戳);LocalDate(⽇期);LocalDate(时间);LocalDateTime(⽇期时间);
ZonedDateTime(带有区域信息的⽇期时间,⽐如中国默认使⽤的是东⼋区)。Period(如两个⽇期之间相差的天数);Druation(两个⽇期时间之间间隔的秒和纳秒)。*/    Instant now = w();
System.out.String());  // 2018-08-06T09:44:13.677Z  (utc时间格式,标准时间格式)
System.out.(ChronoField.MILLI_OF_SECOND)); //毫秒 677
System.out.(ChronoField.MICRO_OF_SECOND)); //微秒 677000
System.out.(ChronoField.NANO_OF_SECOND));  //纳秒 677000000
System.out.println(ZoneId.systemDefault());  // Asia/Shanghai
LocalDateTime localDateTime = LocalDateTime.ofInstant(now, ZoneId.systemDefault());
LocalDate localDate = w();  //
System.out.println(localDate);  //2018-08-06
System.out.println(localDateTime); //2018-08-06T17:44:13.677
// 获得当前⽇期
LocalDate localDate1 = w();
System.out.String());
// ⽇期加上1天
LocalDate localDate2 = localDate1.plusDays(1);
System.out.String());
// ⽇期加上⼀周
LocalDate localDate3 = localDate1.plusWeeks(1);
System.out.println(localDate3);
/
/ 计算当前年的第52天是⼏⽉⼏号
System.out.println("今年的第52天 = " + localDate1.withDayOfYear(52));
// 字符串转⽇期
DateTimeFormatter strToDateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
TemporalAccessor dateTemporal = strToDateFormatter.parse("2017-01-01 13:00:00");
LocalDate date = LocalDate.from(dateTemporal);
System.out.println(date);
LocalDateTime dateTime = LocalDateTime.parse("2017-01-01 13:00:00", strToDateFormatter);
System.out.String());
// 格式化⽇期
LocalDateTime localDateTime1 = w();
DateTimeFormatter dateToStrFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String dateStr = dateToStrFormatter.format(localDateTime1);
System.out.println(dateStr);
// 只处理时间
LocalTime localTime = w();
System.out.println("local time = " + localTime);
System.out.println("plus 12 hours = " + localTime.plusHours(12));
/*Period
Period代表的是两个⽇期之间的天、⽉、年数差值,当然,我们也可以直接使⽤⽇期中的until⽅法来完成同样的效果。*/
LocalDate startDate = w();
LocalDate endDate = startDate.plusDays(1);
Period period = Period.between(startDate, endDate);
System.out.println("间隔的天数" + Days());
System.out.println("间隔的⽉数:" + Months());
System.out.println("间隔的年数:" + Years());
// 直接使⽤⽇期类中的⽅法计算⽇期间隔
long days = startDate.until(endDate, ChronoUnit.DAYS);
System.out.println("间隔的天数:" + days);
long weeks = startDate.until(endDate, ChronoUnit.WEEKS);
System.out.println("间隔的周数:" + weeks);
/*Duration表⽰的是两个⽇期时间间隔的秒以及纳秒数。*/
LocalDateTime start = w();
LocalDateTime end = start.plusDays(1);
Duration duration = Duration.between(start, end);
System.out.println("间隔的秒数:" + (ChronoUnit.SECONDS));
//System.out.println("间隔的毫秒数:" + (ChronoUnit.MICROS));
System.out.println("间隔的纳秒数:" + (ChronoUnit.NANOS));
///////////////////////////////
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String dateTimeF = w().format(fmt); //将当前时间转换为  2017-10-19 10:25:36
//将时间装换为long值时间戳
long currentTime = LocalDateTime.parse(dateTimeF,fmt).atZone(ZoneId.of("Asia/Shanghai")).toInstant().toEpochMilli();    System.out.println("时间戳:"+currentTime);
//将long转为格式化时间字符串
String timeNow = fmt.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTime),ZoneId.of("Asia/Shanghai")));    System.out.println("时间:"+timeNow);
/* Date转LocalDate:*/
Date date11 = new Date();
LocalDate localDate11 = Instant().atZone(ZoneId.systemDefault()).toLocalDate();
System.out.println(localDate11);
/*  LocalDate 转 Date:*/
LocalDateTime localDateTime22 = w();
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zdt = localDateTime22.atZone(zoneId);
Date dateType = Date.Instant());
//Date dateType = Date.from(localDateTime22.atZone(ZoneId.systemDefault()).toInstant());
System.out.println("LocalDateTime = " + localDateTime22);
System.out.println("Date = " + dateType);
//jdk 1.7
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("MM/dd/yyyy");
Date date33 = sdf.parse("2018-08-08");
String aaa = sdf2.format(date33);
System.out.println(aaa);
}
public static List<String> getDateArrayList(String startTime, String endTime) throws ParseException{
List<String> dateArrayList = new ArrayList<String>();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
long stime=sdf.parse(startTime).getTime();
long etime=sdf.parse(endTime).getTime();
while(stime<=etime){
String time=sdf.format(new Date(stime));
dateArrayList.add(time);
stime += 24*60*60*1000;
}
return dateArrayList;
}
string转date的方法}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。