java周期_java 周期时期计算
package org.apple.date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 周期時(shí)間
* @author shaoyu
*
*/
public class CycleDate {
public static void main(String[] args) {
SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
Calendar calendar = Calendar.getInstance();
//calendar.setTime(new Date());
System.out.println(calendar.get(Calendar.YEAR));
System.out.println(calendar.get(Calendar.MONTH)+1);// 月加1
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("當(dāng)月第幾周:"+calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println("當(dāng)年第幾周:"+calendar.get(Calendar.WEEK_OF_YEAR));
System.out.println(calendar.get(Calendar.DAY_OF_WEEK)-1);// 周減1
int hour_24 = calendar.get(Calendar.HOUR_OF_DAY);
int hour_12 = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
System.out.println("24小時(shí)制 "+hour_24 +":"+ minute +":" + second);
System.out.println("12小時(shí)制 "+hour_12 +":"+ minute +":" + second);
System.out.println("當(dāng)前時(shí)間:"+dateformat.format(calendar.getTime()) + " || 毫秒數(shù):"+ calendar.getTimeInMillis());
calendar.add(Calendar.MONTH, 1);
System.out.println("下一月當(dāng)前時(shí)間:"+dateformat.format(calendar.getTime()) + " || 毫秒數(shù):"+ calendar.getTimeInMillis());
calendar.add(Calendar.WEEK_OF_MONTH, 1);
System.out.println("下一周當(dāng)前時(shí)間:" + dateformat.format(calendar.getTime()) + " || 毫秒數(shù):"+ calendar.getTimeInMillis());
calendar.add(Calendar.YEAR, 1);
System.out.println("下一年當(dāng)前時(shí)間:" + dateformat.format(calendar.getTime()) + " || 毫秒數(shù):"+ calendar.getTimeInMillis());
//System.out.println("季度開(kāi)始時(shí)間 : "+ dateformat.format(getCurrentQuarterStartTime()));
System.out.println("下一季度結(jié)果時(shí)間: " + dateformat.format(getCurrentQuarterEndTime(calendar.get(Calendar.MONTH)+4)));
}
public Date lastDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.roll(Calendar.DAY_OF_MONTH, -1);
return calendar.getTime();
}
/**
* 當(dāng)前季度的開(kāi)始時(shí)間
*/
public static Date getCurrentQuarterStartTime() {
SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar c = Calendar.getInstance();
int currentMonth = c.get(Calendar.MONTH) + 1;
Date now = null;
try {
if (currentMonth >= 1 && currentMonth <= 3)
c.set(Calendar.MONTH, 0);
else if (currentMonth >= 4 && currentMonth <= 6)
c.set(Calendar.MONTH, 3);
else if (currentMonth >= 7 && currentMonth <= 9)
c.set(Calendar.MONTH, 6);
else if (currentMonth >= 10 && currentMonth <= 12)
c.set(Calendar.MONTH, 9);
c.set(Calendar.DATE, 1);
now = dateformat.parse(dateformat.format(c.getTime()) + " 00:00");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 當(dāng)前季度的結(jié)束時(shí)間
*/
public static Date getCurrentQuarterEndTime(int currentMonth) {
SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar c = Calendar.getInstance();
Date now = null;
try {
if (currentMonth >= 1 && currentMonth <= 3) {
c.set(Calendar.MONTH, 2);
c.set(Calendar.DATE, 31);
} else if (currentMonth >= 4 && currentMonth <= 6) {
c.set(Calendar.MONTH, 5);
c.set(Calendar.DATE, 30);
} else if (currentMonth >= 7 && currentMonth <= 9) {
c.set(Calendar.MONTH, 8);
c.set(Calendar.DATE, 30);
} else if (currentMonth >= 10 && currentMonth <= 12) {
c.set(Calendar.MONTH, 11);
c.set(Calendar.DATE, 31);
}
now = dateformat.parse(dateformat.format(c.getTime()) + " 23:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
}
原文:http://www.cnblogs.com/shaoyu19900421/p/4489236.html
總結(jié)
以上是生活随笔為你收集整理的java周期_java 周期时期计算的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: c语言中case1 case3 n =1
- 下一篇: python日期转化成周数_[转]浅析使