public static int getWeekOfYear(Stringdate){
try{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
//这一句必须要设置,否则美国认为第一天是周日,而我国认为是周一,对计算当期日期是第几周会有错误
cal.setFirstDayOfWeek(Calendar.MONDAY); // 设置每周的第一天为星期一
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);// 每周从周一开始
cal.setMinimalDaysInFirstWeek(1);// 设置每周最少为1天
cal.setTime(df.parse(date));
return cal.get(Calendar.WEEK_OF_YEAR);
}
catch(Exception e){
e.printStackTrace();
}
return 0;
}
示例代码:
WeekCalendar.java
---------------------------------------------------
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class WeekCalendar{
public static String weekdatetodata(int year, intmonth, int weekOfMonth, int dayOfWeek){
Calendar c =Calendar.getInstance();
//计算出 x年 y月 1号 是星期几
c.set(year, month - 1, 1);
//如果i_week_day =1的话实际上是周日
int i_week_day =c.get(Calendar.DAY_OF_WEEK);
int sumDay = 0;
//dayOfWeek+1 就是星期几(星期日 为1)
if(i_week_day == 1){
sumDay =(weekOfMonth - 1) * 7 + dayOfWeek + 1;
}
else{
sumDay = 7 -i_week_day + 1 + (weekOfMonth - 1) * 7 + dayOfWeek + 1;
}
//在1号的基础上加上相应的天数
c.set(Calendar.DATE,sumDay);
SimpleDateFormat sf2 = newSimpleDateFormat("yyyy-MM-dd");
returnsf2.format(c.getTime());
}
public static int getWeekOfYear(String date){
try{
SimpleDateFormatdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal= Calendar.getInstance();
//这一句必须要设置,否则美国认为第一天是周日,而我国认为是周一,对计算当期日期是第几周会有错误
cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置每周的第一天为星期一
cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);// 每周从周一开始
cal.setMinimalDaysInFirstWeek(1);// 设置每周最少为1天
cal.setTime(df.parse(date));
returncal.get(Calendar.WEEK_OF_YEAR);
}
catch(Exception e){
e.printStackTrace();
}
return 0;
}
public static int getWeekOfYear(Date date){
try{
Calendar cal= Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置每周的第一天为星期一
cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);// 每周从周一开始
cal.setMinimalDaysInFirstWeek(1);// 设置每周最少为1天
cal.setTime(date);
returncal.get(Calendar.WEEK_OF_YEAR);
}
catch(Exception e){
e.printStackTrace();
}
return 0;
}
public static Calendar CalendarSetTime(StringsCurrDate){
Calendar oCalendar =Calendar.getInstance();
Date oDate = new Date();
SimpleDateFormat sdf = newSimpleDateFormat("yyyy-MM-dd");
if(!sCurrDate.equals("")&& sCurrDate != null){
try{
oCalendar.setTime(sdf.parse(sCurrDate));
}
catch(Exceptionex){
ex.printStackTrace(System.err);
System.out.println("日期格式转换错误:="+ sCurrDate);
}
}
else
oCalendar.setTime(oDate);
return oCalendar;
}
public static List Calendar(StringsCurrDate){
String[] oneWeekDay = {"星期日","星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar oCal = null;
String LoopDate = "";
int iweek = 0, iCurrWeek =0;
oCal =CalendarSetTime(sCurrDate);//设置时间
iweek =oCal.get(Calendar.DAY_OF_WEEK) - 1;
String DataSet[] = null;
ArrayList<String[]>DayAndWeek = new ArrayList();
for(int i = iweek; i> 0; i--){
oCal.add(Calendar.DATE,-i);
LoopDate =oCal.get(Calendar.YEAR) + "-" + (oCal.get(Calendar.MONTH) + 1) +"-" + oCal.get(Calendar.DATE);
oCal =CalendarSetTime(sCurrDate);//重置时间
DataSet = newString[2];
DataSet[0] =LoopDate;
DataSet[1] =oneWeekDay[(iCurrWeek++)];
DayAndWeek.add(DataSet);
}
for(int i = 0; i< 7 - iweek; i++){
oCal.add(Calendar.DATE,i);
LoopDate =oCal.get(Calendar.YEAR) + "-" + (oCal.get(Calendar.MONTH) + 1) +"-" + oCal.get(Calendar.DATE);
oCal =CalendarSetTime(sCurrDate);
DataSet = newString[2];
DataSet[0] =LoopDate;
DataSet[1] =oneWeekDay[(iCurrWeek++)];
DayAndWeek.add(DataSet);
}
return DayAndWeek;
}
public static void main(String[] args) throwsParseException{
//2009年1月第二周星期三得到2009-01-14
//System.out.println(weekdatetodata(2009,1, 2, 3));
//2009年2月第二周星期三得到2009-02-11
//System.out.println(weekdatetodata(2009,2, 2, 3));
//2009年4月第二周星期三得到2009-04-15
//System.out.println(weekdatetodata(2009,4, 2, 3));
//2009年2月第三周星期四得到2009-02-19
//System.out.println(weekdatetodata(2009,2, 3, 4));
//System.out.println("2012-1-1第几周:"+getWeekOfYear("2012-1-1"));
//System.out.println("2012-1-2第几周:"+getWeekOfYear("2012-1-2"));
//System.out.println("2012-1-8第几周:"+getWeekOfYear("2012-1-8"));
//System.out.println("2012-1-9第几周:"+getWeekOfYear("2012-1-9"));
DateObj d = getDateObj(newDate(System.currentTimeMillis()));
System.out.println(d);
}
public static DateObj getDateObj(Datemydate){
DateObj obj = newDateObj();
if(null != mydate){
SimpleDateFormatsformat = new SimpleDateFormat("yyyy-MM-dd");
StringdateString = sformat.format(mydate);
List<String[]>list = Calendar(dateString);
String[]firstday = list.get(0);
String[]lastday = list.get(list.size() - 1);
obj.setCurrentDate(mydate);
obj.setFirstDay(firstday[0]);
obj.setLastDay(lastday[0]);
obj.setWeekOfYear(getWeekOfYear(dateString));
}
return obj;
}
}
DateObj.java
---------------------------------------------------
import java.util.Date;
public class DateObj{
private String firstDay,lastDay;
private Date currentDate;
public Date getCurrentDate(){
return currentDate;
}
public void setCurrentDate(DatecurrentDate){
this.currentDate = currentDate;
}
private int weekOfYear;
public String getFirstDay(){
return firstDay;
}
public void setFirstDay(String firstDay){
this.firstDay = firstDay;
}
public String getLastDay(){
return lastDay;
}
public void setLastDay(String lastDay){
this.lastDay = lastDay;
}
public int getWeekOfYear(){
return weekOfYear;
}
public void setWeekOfYear(int weekOfYear){
this.weekOfYear = weekOfYear;
}
@Override
publicString toString(){
return "DateObj [firstDay=" + firstDay + ", lastDay=" + lastDay +", currentDate=" + currentDate
+ ", weekOfYear=" + weekOfYear + "]";
}
}
安卓开发中,设置EditText控件的属性:
android:singleLine="true"
设置单行输入,一旦设置,则文字不会换行。
java计算昨天和明天的方法
Date date = new Date();//取时间
Calendar calendar = newGregorianCalendar();
calendar.setTime(date);
calendar.add(calendar.DATE,-1);//把日期往后增加一天.整数往后推,负数往前移动
date = calendar.getTime();//这个时间就是日期往后推一天的结果
SimpleDateFormat formatter =new SimpleDateFormat("yyyy-MM-dd");
String dateString =formatter.format(date);
System.out.println(dateString);