アプリ作って海外移住

コピペプログラマーのメモ

日付

 /////////////yyyy/mm/ddの文字列からcalendarクラス作成

①DateFormatでyyyy/mm/ddを設定

②Date に parseする。

③カレンダーにセットcal.setTime(date); 

try{
//日付情報の初期設定

DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
Date date = df.parse(IP_date);

Calendar cal = Calendar.getInstance();
cal.setTime(date);
ZS_YYYY = cal.get(Calendar.YEAR);
ZS_MM = cal.get(Calendar.MONTH);
ZS_DD = cal.get(Calendar.DAY_OF_MONTH);

}catch (ParseException e) {
ZS_YYYY = 1970;
ZS_MM = 0;
ZS_DD = 1;
}

 

 

 

 

 

 

 

 

 

 

 

日数差

  //2013/12/1 から2014 2015  2016/1/31 の仕様
        Calendar cal_sta = Calendar.getInstance();      
        cal_sta.set(2013,11,1);
        Calendar cal_end = Calendar.getInstance();      
        cal_end.set(2016,0,31);
        long n = (cal_end.getTimeInMillis()/(1000*60*60*24))
                  -(cal_sta.getTimeInMillis()/(1000*60*60*24));
       
        int nisuu = (int)n+1;

 

Calendar cal = Calendar.getInstance();   

//////////////////////////曜日
        int week = cal.get(Calendar.DAY_OF_WEEK);
        String[] wd_array = {"日","月","火","水","木","金","土"};
        String youbi = wd_array[week-1]+"曜日";
        TextView tv_weekday=(TextView)this.findViewById(R.id.tv_weekday);
        tv_weekday.setText(youbi);

        //////////////////////////年
         int year = cal.get(Calendar.YEAR);
         String yy = ""+year+"年";
         TextView tv_yy=(TextView)this.findViewById(R.id.tv_yy);
         tv_yy.setText(yy);
         
         
         //////////////////////////月日
         
          int month = cal.get(Calendar.MONTH)+1;
          int day = cal.get(Calendar.DAY_OF_MONTH);
         
          String mmdd = month+"月"+day+"日";
         TextView tv_mmdd=(TextView)this.findViewById(R.id.tv_mmdd);
         tv_mmdd.setText(mmdd);

 

 

 

 

 

//現在時刻取得////////////////////////////////////////////////////////////////

  Calendar cal = Calendar.getInstance();    
       

//この形に変換

   //SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm"); ①

   SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");      ② 
        String s = format.format(cal.getTime());
       
        TextView tv=(TextView)this.findViewById(R.id.textView1);
         tv.setText(s);

結果

----① 2014/02/03 12:59

----② 2014/02/03 

 

 

//指定時刻の場合

注意:月は、0-11で表されるため、5月を指定したい場合は、1減算した4を指定する必要がある

以下は4月になる。

Calendar cal = Calendar.getInstance(); 

cal.set(2012,03,19,11,23,59);

SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");    
 String s = format.format(cal.getTime());
       
 TextView tv=(TextView)this.findViewById(R.id.textView1);
 tv.setText(s);

 

 

 

//////////////////////////////曜日 1=日 2=月 ・・・なので注意

String[] wd_array = {"日","月","火","水","木","金","土"};
       
        Calendar cal = Calendar.getInstance();
        //cal.set(2012,03,19,11,23,59);    //任意指定の場合
        //SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        String s = format.format(cal.getTime());
        int week = cal.get(Calendar.DAY_OF_WEEK);
        s = s + wd_array[week-1];
       
        TextView textView4=(TextView)this.findViewById(R.id.textView1);
         textView4.setText(s);
結果

2014/2/14月

 

DatePickerからの設定は、
DatePicker dp = (DatePicker)findViewById(R.id.datePicker1);
cal.set(dp.getYear(), dp.getMonth(), dp.getDayOfMonth());

 

cal.set(Calendar.DAY_OF_MONTH, 1); 月の1日目の日付
cal.add(Calendar.MONTH, 1);     翌月の1日の日付  

 

 

 

1日前の取得

 Calendar c = Calendar.getInstance();
c.set(2006, 0, 1); // 2006年1月1日を設定
c.add(Calendar.DATE, -1); // 1日前とする
System.out.println(c.getTime()); // 2005年12月31日と表示される