Android判断当前时间为本月的第几周,本周的第几天
jopen
11年前
import java.text.SimpleDateFormat; import java.util.Calendar; import android.app.Activity; import android.os.Bundle; /** * Demo描述: * 判断当前时间为本月的第几周 * 判断当前时间为本周的第几天 * 判断当前的季节 * * 注意问题: * 在Calendar中每周是从我们中国人的周日(星期七)开始计算的. * 所以Calendar的周一实际为我们中国人的上周的星期七. * 在此需要特殊处理一下. * */ public class TestWeekActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); getCurrentTime(); getWeekAndDay(); } private void getWeekAndDay(){ Calendar calendar= Calendar.getInstance(); //获取当前时间为本月的第几周 int week = calendar.get(Calendar.WEEK_OF_MONTH); //获取当前时间为本周的第几天 int day = calendar.get(Calendar.DAY_OF_WEEK); if (day==1) { day=7; week=week-1; } else { day=day-1; } System.out.println("今天是本月的第" + week + "周"+",星期"+(day)); } //获取当前时间(精确到天)且判断季节 private void getCurrentTime() { SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd"); String date = sDateFormat.format(new java.util.Date()); System.out.println("date=" + date); String dates[] = date.split("-"); System.out.println("dates[0]=" + dates[0]); System.out.println("dates[1]=" + dates[1]); System.out.println("dates[2]=" + dates[2]); System.out.println("---------------------"); System.out.println("dates[0]=" + Integer.valueOf(dates[0])); System.out.println("dates[1]=" + Integer.valueOf(dates[1])); System.out.println("dates[2]=" + Integer.valueOf(dates[2])); int month = Integer.valueOf(dates[1]); String season = null; if (month == 12 || month == 1 || month == 2) { season="冬天"; } else if(month == 3 || month == 4 || month == 5){ season="春天"; } else if(month == 6 || month == 7 || month == 8) { season="夏天"; }else { season="秋天"; } System.out.println("季节:"+season); } }