Android的日期选取器格式 [英] Android Date Picker Formatting
本文介绍了Android的日期选取器格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图让这种格式的日期星期五2011年10月12。我是新来的日期时间选择器,发现这code网上。当我修改我得到以下输出2013年4月17日15时44分33秒,但我希望得到星期二2013年4月17日。下面是日期选取器类
公共类的DateTimePicker实现OnClickListener
{
私人的DatePicker日期选择器;
私人TimePicker timePicker;
私人ViewSwitcher viewSwitcher; 私人最终诠释SET_DATE = 100,SET_TIME = 101,SET = 102,CANCEL = 103; 私人按钮btn_setDate,btn_setTime,btn_set,btn_cancel; 私人日历CALENDAR_DATE = NULL; 私人活动活动; 私人ICustomDateTimeListener iCustomDateTimeListener = NULL; 私人对话的对话; 私人布尔is24HourView = TRUE; 公众的DateTimePicker(活动,ICustomDateTimeListener customDateTimeListener)
{
活性=一个;
iCustomDateTimeListener = customDateTimeListener; 对话框=新的对话框(活动);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
查看dialogView = getDateTimePickerLayout();
dialog.setContentView(dialogView);
} 公共查看getDateTimePickerLayout()
{
LinearLayout.LayoutParams linear_match_wrap =新LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
LinearLayout.LayoutParams linear_wrap_wrap =新LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
FrameLayout.LayoutParams frame_match_wrap =新FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT); LinearLayout.LayoutParams button_params =新LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT,1.0F); 的LinearLayout linear_main =新的LinearLayout(活动);
linear_main.setLayoutParams(linear_match_wrap);
linear_main.setOrientation(LinearLayout.VERTICAL);
linear_main.setGravity(Gravity.CENTER); 的LinearLayout linear_child =新的LinearLayout(活动);
linear_child.setLayoutParams(linear_wrap_wrap);
linear_child.setOrientation(LinearLayout.VERTICAL); 的LinearLayout linear_top =新的LinearLayout(活动);
linear_top.setLayoutParams(linear_match_wrap); btn_setDate =新按钮(活动);
btn_setDate.setLayoutParams(button_params);
btn_setDate.setText(设定日期);
btn_setDate.setId(SET_DATE);
btn_setDate.setOnClickListener(本);
btn_setDate.setVisibility(View.GONE);
// 55
btn_setTime =新按钮(活动);
btn_setTime.setLayoutParams(button_params);
btn_setTime.setText(设置时间);
btn_setTime.setId(SET_TIME);
btn_setTime.setOnClickListener(本);
btn_setTime.setVisibility(View.GONE); linear_top.addView(btn_setDate);
linear_top.addView(btn_setTime); viewSwitcher =新ViewSwitcher(活动);
viewSwitcher.setLayoutParams(frame_match_wrap); 日期选择器=新的DatePicker(活动);
timePicker =新TimePicker(活动); viewSwitcher.addView(timePicker);
viewSwitcher.addView(日期选择器); 的LinearLayout linear_bottom =新的LinearLayout(活动);
linear_match_wrap.topMargin = 8;
linear_bottom.setLayoutParams(linear_match_wrap); btn_set =新按钮(活动);
btn_set.setLayoutParams(button_params);
btn_set.setText(设置);
btn_set.setId(SET);
btn_set.setOnClickListener(本); btn_cancel =新按钮(活动);
btn_cancel.setLayoutParams(button_params);
btn_cancel.setText(取消);
btn_cancel.setId(取消);
btn_cancel.setOnClickListener(本); linear_bottom.addView(btn_set);
linear_bottom.addView(btn_cancel); linear_child.addView(linear_top);
linear_child.addView(viewSwitcher);
linear_child.addView(linear_bottom); linear_main.addView(linear_child); 返回linear_main;
} 公共无效的ShowDialog()
{
如果(!dialog.isShowing())
{
如果(CALENDAR_DATE == NULL)
CALENDAR_DATE = Calendar.getInstance(); timePicker.setIs24HourView(is24HourView);
timePicker.setCurrentHour(calendar_date.get(Calendar.HOUR_OF_DAY));
timePicker.setCurrentMinute(calendar_date.get(Calendar.MINUTE)); datePicker.updateDate(calendar_date.get(Calendar.YEAR),calendar_date.get(的Calendar.MONTH),calendar_date.get(Calendar.DATE)); dialog.show(); btn_setDate.performClick();
}
} 公共无效dismissDialog()
{
如果(!dialog.isShowing())
dialog.dismiss();
} 公共无效的setDate(日历日历)
{
如果(日历!= NULL)
CALENDAR_DATE =日历;
} 公共无效的setDate(日期为准)
{
如果(日期!= NULL)
{
CALENDAR_DATE = Calendar.getInstance();
calendar_date.setTime(日期);
}
} 公共无效的setDate(INT年,月整型,诠释天)
{
如果(月< 12安培;&安培;当月> = 0&放大器;和天< 32安培;和天> = 0&放大器;&安培;一年> 100安培;&安培;一年< 3000)
{
CALENDAR_DATE = Calendar.getInstance();
calendar_date.set(年,月,日);
} }
公共无效setTimeIn24HourFormat(INT hourIn24Format,分整型)
{
如果(hourIn24Format< 24安培;&安培; hourIn24Format> = 0&放大器;&安培;&分钟GT; = 0&放大器;&安培;&分钟LT; 60)
{
如果(CALENDAR_DATE == NULL)
CALENDAR_DATE = Calendar.getInstance(); calendar_date.set(calendar_date.get(Calendar.YEAR),calendar_date.get(的Calendar.MONTH),calendar_date.get(Calendar.DAY_OF_MONTH),hourIn24Format,分钟); is24HourView = TRUE;
}
} 公共无效setTimeIn12HourFormat(INT hourIn12Format,INT分钟,布尔ISAM)
{
如果(hourIn12Format&; 13放大器;&放大器; hourIn12Format大于0&放大器;&放大器;分钟> = 0&放大器;&放大器;分钟&下; 60)
{
如果(hourIn12Format == 12)
hourIn12Format = 0; INT hourIn24Format = hourIn12Format; 如果(!ISAM)
hourIn24Format + = 12; 如果(CALENDAR_DATE == NULL)
CALENDAR_DATE = Calendar.getInstance(); calendar_date.set(calendar_date.get(Calendar.YEAR),calendar_date.get(的Calendar.MONTH),calendar_date.get(Calendar.DAY_OF_MONTH),hourIn24Format,分钟); is24HourView = FALSE;
}
} 公共无效set24HourFormat(布尔is24HourFormat)
{
is24HourView = is24HourFormat;
} 公共接口ICustomDateTimeListener
{
公共无效发作(日历calendarSelected,日期dateSelected,年整型,字符串monthFullName,字符串monthShortName,诠释monthNumber,诠释日期,字符串weekDayFullName,字符串weekDayShortName);
公共无效onCancel();
}
@覆盖
公共无效的onClick(视图v)
{
开关(v.getId())
{
案例SET_DATE:btn_setTime.setEnabled(真);
btn_setDate.setEnabled(假);
viewSwitcher.showNext();
打破; / *案例SET_TIME:btn_setTime.setEnabled(假);
btn_setDate.setEnabled(真);
viewSwitcher.show previous();
打破;
* /
案例集:如果(dialog.isShowing())
dialog.dismiss();
如果(iCustomDateTimeListener!= NULL)
{
INT月= datePicker.getMonth();
年整型= datePicker.getYear();
INT天= datePicker.getDayOfMonth(); calendar_date.set(年,月,日); iCustomDateTimeListener.onSet(CALENDAR_DATE,calendar_date.getTime(),calendar_date.get(Calendar.YEAR),getMonthFullName(calendar_date.get(的Calendar.MONTH)),getMonthShortName(calendar_date.get(的Calendar.MONTH)),calendar_date.get(日历。月), calendar_date.get(Calendar.DAY_OF_MONTH),getWeekDayFullName(calendar_date.get(Calendar.DAY_OF_WEEK)),getWeekDayShortName(calendar_date.get(Calendar.DAY_OF_WEEK)));
}
// resetData();
打破; 案例取消:如果(dialog.isShowing())
dialog.dismiss();
如果(iCustomDateTimeListener!= NULL)
iCustomDateTimeListener.onCancel();
resetData();
打破;
}
}
/ **
* @参数日期日期字符串
你和LT的* @参数fromFormat格式; B>日期和LT; / B>例如:如果你的日期是2011-07-07 9时09分09秒,然后您的格式将是< B> YYYY-MM-DD HH:MM:SS< / B>
* @参数toFormat格式要转换您的< B>日期和LT; / B>例如:如果需要的格式是2011 7月31日则toFormat应该是< B> d MMMM YYYY< / B>
* @返回格式化的日期
* /
公共静态字符串convertDate(日期字符串,字符串fromFormat,字符串toFormat)
{
串formattedDate =;
尝试
{
SimpleDateFormat的SimpleDateFormat的=新的SimpleDateFormat(fromFormat);
日期D = simpleDateFormat.parse(日期);
台历挂历= Calendar.getInstance();
calendar.setTime(四); SimpleDateFormat的=新的SimpleDateFormat(toFormat);
simpleDateFormat.setCalendar(日历);
formattedDate = simpleDateFormat.format(calendar.getTime()); }赶上(例外五)
{
如果(E!= NULL)
e.printStackTrace();
} 返回formattedDate;
} / **
* @参数monthNumber月份数字开始以0为< B> 1< / B>它为< B个0℃/ B个和< B>十二月< / B>它为&lt b取代; 11&下; / B取代。
* @返回
* /
私人字符串getMonthFullName(INT monthNumber)
{
字符串MONTHNAME =; 如果(monthNumber> = 0&放大器;&放大器; monthNumber&下; 12)
尝试
{
台历挂历= Calendar.getInstance();
calendar.set(的Calendar.MONTH,monthNumber); SimpleDateFormat的SimpleDateFormat的=新的SimpleDateFormat(MMMM);
simpleDateFormat.setCalendar(日历);
MONTHNAME = simpleDateFormat.format(calendar.getTime());
}赶上(例外五)
{
如果(E!= NULL)
e.printStackTrace();
} 返回MONTHNAME;
} / **
* @参数monthNumber月开始的数字为0。< B> 1< / B>它为< B个0℃/ B个和< B>十二月< / B>它为&lt b取代; 11&下; / B取代。
* @返回
* /
私人字符串getMonthShortName(INT monthNumber)
{
字符串MONTHNAME =; 如果(monthNumber> = 0&放大器;&放大器; monthNumber&下; 12)
尝试
{
台历挂历= Calendar.getInstance();
calendar.set(的Calendar.MONTH,monthNumber); SimpleDateFormat的SimpleDateFormat的=新的SimpleDateFormat(MMM);
simpleDateFormat.setCalendar(日历);
MONTHNAME = simpleDateFormat.format(calendar.getTime());
}
赶上(例外五)
{
如果(E!= NULL)
e.printStackTrace();
}
返回MONTHNAME;
} / **
* @参数weekDayNumber周数从1开始为< B>星期日及LT; / B>它为&lt b取代1所述; / B个和< B>周六和LT; / B>它为&lt b取代; 7所述; / B取代。
* @返回
* /
私人字符串getWeekDayFullName(INT weekDayNumber)
{
串weekName =; 如果(weekDayNumber大于0&放大器;&放大器; weekDayNumber&下; 8)
{
尝试{
台历挂历= Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK,weekDayNumber); SimpleDateFormat的SimpleDateFormat的=新的SimpleDateFormat(EEEE);
simpleDateFormat.setCalendar(日历);
weekName = simpleDateFormat.format(calendar.getTime());
}赶上(例外五)
{
如果(E!= NULL)
e.printStackTrace();
}
}
返回weekName;
} / **
* @参数weekDayNumber周数从1开始为< B>星期日及LT; / B>它为&lt b取代1所述; / B个和< B>周六和LT; / B>它为&lt b取代; 7所述; / B取代。
* @返回
* /
私人字符串getWeekDayShortName(INT weekDayNumber)
{
串weekName =;
如果(weekDayNumber大于0&放大器;&放大器; weekDayNumber&下; 8)
{
尝试{
台历挂历= Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK,weekDayNumber); SimpleDateFormat的SimpleDateFormat的=新的SimpleDateFormat(EE);
simpleDateFormat.setCalendar(日历);
weekName = simpleDateFormat.format(calendar.getTime());
}赶上(例外五){
如果(E!= NULL)
e.printStackTrace();
}
}
返回weekName;
} 私人诠释getHourIn12Format(INT hour24)
{
INT hourIn12Format = 0; 如果(hour24 == 0)
hourIn12Format = 12;
否则,如果(hour24< = 12)
hourIn12Format = hour24;
其他
hourIn12Format = hour24-12; 返回hourIn12Format;
} 私人字符串getAMPM(日历日历)
{
字符串AMPM =(calendar.get(Calendar.AM_PM)==(Calendar.AM))? 上午下午;
返回的ampm;
} 私人无效resetData()
{
CALENDAR_DATE = NULL;
is24HourView = TRUE;
} 公共静态字符串垫(int i)以
{
返回I< = 90+我:+ I;
} 公共静态字符串getSecondsFromMillis(长毫秒){
回归+((INT)((毫秒/ 1000)%60));
} 公共静态字符串getMinutesFromMillis(长毫秒){
返回+(INT)((毫秒/(1000 * 60))%60);
} 公共静态字符串getHoursFromMillis(长毫秒){
返回+(INT)((毫秒/(1000 * 60 * 60))%24);
} / **
* @参数monthNumber月开始的数字为0。< B> 1< / B>它为< B个0℃/ B个和< B>十二月< / B>它为&lt b取代; 11&下; / B取代。
* @参数年
* @返回
* /
公共静态INT getDaysInMonth(INT monthNumber,INT年)
{
INT天= 0;
如果(monthNumber> = 0&放大器;&放大器; monthNumber&下; 12){
尝试
{
台历挂历= Calendar.getInstance();
INT日期= 1;
calendar.set(年,monthNumber,日期);
天= calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}赶上(例外五)
{
如果(E!= NULL)
e.printStackTrace();
}
}
回天;
} / **
* @参数monthNumber月开始的数字为0。< B> 1< / B>它为< B个0℃/ B个和< B>十二月< / B>它为&lt b取代; 11&下; / B取代。
* @返回
* /
公共静态INT getDaysInMonthIn presentYear(INT monthNumber)
{
INT天= 0;
如果(monthNumber> = 0&放大器;&放大器; monthNumber&下; 12){
尝试
{
台历挂历= Calendar.getInstance();
INT日期= 1;
年整型= calendar.get(Calendar.YEAR);
calendar.set(年,monthNumber,日期);
天= calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}赶上(例外五)
{
如果(E!= NULL)
e.printStackTrace();
}
}
回天;
} 公共静态INT getDaysDifference(日期寄件者,日期TODATE)
{
如果(寄件者== NULL || TODATE == NULL)
返回0; 回报(INT)((toDate.getTime() - fromDate.getTime())/(1000 * 60 * 60 * 24));
} 公共静态INT getDaysDifference(日历CALENDAR1,日历CALENDAR2)
{
如果(CALENDAR1 == NULL || CALENDAR2 == NULL)
返回0; 回报(INT)((calendar2.getTimeInMillis() - calendar1.getTimeInMillis())/(1000 * 60 * 60 * 24));
}}
我在我的MainActivity.Class调用它,如下所示:
公共类MainActivity扩展活动实现ICustomDateTimeListener
{
私人TextView的TextView的;
私人的DateTimePicker dateTimePicker的; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); TextView的=(的TextView)findViewById(R.id.textview); dateTimePicker的=新的DateTimePicker(MainActivity.this,这一点);
dateTimePicker.set24HourFormat(真);
dateTimePicker.showDialog();
//存款准备金率
} @覆盖
公共无效发作(日历calendarSelected,日期dateSelected,年整型,
字符串monthFullName,字符串monthShortName,INT monthNumber,
INT日期,字符串weekDayFullName,字符串weekDayShortName)
{
textView.setText(dateSelected.toLocaleString());
} @覆盖
公共无效onCancel()
{
Log.d(datetimepickerdialog,取消);
}
解决方案
您必须在活动的发病方法做到这一点:
的SimpleDateFormat格式化=新的SimpleDateFormat(EEEE DD MMM YYYY);
textView.setText(formatter.format(dateSelected));
I am trying to get date in this format "Friday 12 October 2011" from the DateTimePicker Class below. I am new to date time picker and found this code online. When i modify it i get the following output "17 apr. 2013 15:44:33" but i want to get "Tuesday 17 apr. 2013". Here is the Date Picker class
public class DateTimePicker implements OnClickListener
{
private DatePicker datePicker;
private TimePicker timePicker;
private ViewSwitcher viewSwitcher;
private final int SET_DATE=100,SET_TIME=101,SET=102,CANCEL=103;
private Button btn_setDate,btn_setTime,btn_set,btn_cancel;
private Calendar calendar_date=null;
private Activity activity;
private ICustomDateTimeListener iCustomDateTimeListener = null;
private Dialog dialog;
private boolean is24HourView = true;
public DateTimePicker(Activity a,ICustomDateTimeListener customDateTimeListener)
{
activity = a;
iCustomDateTimeListener = customDateTimeListener;
dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
View dialogView = getDateTimePickerLayout();
dialog.setContentView(dialogView);
}
public View getDateTimePickerLayout()
{
LinearLayout.LayoutParams linear_match_wrap = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
LinearLayout.LayoutParams linear_wrap_wrap = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
FrameLayout.LayoutParams frame_match_wrap = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams button_params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1.0f);
LinearLayout linear_main = new LinearLayout(activity);
linear_main.setLayoutParams(linear_match_wrap);
linear_main.setOrientation(LinearLayout.VERTICAL);
linear_main.setGravity(Gravity.CENTER);
LinearLayout linear_child = new LinearLayout(activity);
linear_child.setLayoutParams(linear_wrap_wrap);
linear_child.setOrientation(LinearLayout.VERTICAL);
LinearLayout linear_top = new LinearLayout(activity);
linear_top.setLayoutParams(linear_match_wrap);
btn_setDate = new Button(activity);
btn_setDate.setLayoutParams(button_params);
btn_setDate.setText("Set Date");
btn_setDate.setId(SET_DATE);
btn_setDate.setOnClickListener(this);
btn_setDate.setVisibility(View.GONE);
//55
btn_setTime = new Button(activity);
btn_setTime.setLayoutParams(button_params);
btn_setTime.setText("Set Time");
btn_setTime.setId(SET_TIME);
btn_setTime.setOnClickListener(this);
btn_setTime.setVisibility(View.GONE);
linear_top.addView(btn_setDate);
linear_top.addView(btn_setTime);
viewSwitcher = new ViewSwitcher(activity);
viewSwitcher.setLayoutParams(frame_match_wrap);
datePicker = new DatePicker(activity);
timePicker = new TimePicker(activity);
viewSwitcher.addView(timePicker);
viewSwitcher.addView(datePicker);
LinearLayout linear_bottom = new LinearLayout(activity);
linear_match_wrap.topMargin = 8;
linear_bottom.setLayoutParams(linear_match_wrap);
btn_set = new Button(activity);
btn_set.setLayoutParams(button_params);
btn_set.setText("Set");
btn_set.setId(SET);
btn_set.setOnClickListener(this);
btn_cancel = new Button(activity);
btn_cancel.setLayoutParams(button_params);
btn_cancel.setText("Cancel");
btn_cancel.setId(CANCEL);
btn_cancel.setOnClickListener(this);
linear_bottom.addView(btn_set);
linear_bottom.addView(btn_cancel);
linear_child.addView(linear_top);
linear_child.addView(viewSwitcher);
linear_child.addView(linear_bottom);
linear_main.addView(linear_child);
return linear_main;
}
public void showDialog()
{
if(!dialog.isShowing())
{
if(calendar_date==null)
calendar_date = Calendar.getInstance();
timePicker.setIs24HourView(is24HourView);
timePicker.setCurrentHour(calendar_date.get(Calendar.HOUR_OF_DAY));
timePicker.setCurrentMinute(calendar_date.get(Calendar.MINUTE));
datePicker.updateDate(calendar_date.get(Calendar.YEAR),calendar_date.get(Calendar.MONTH), calendar_date.get(Calendar.DATE));
dialog.show();
btn_setDate.performClick();
}
}
public void dismissDialog()
{
if(!dialog.isShowing())
dialog.dismiss();
}
public void setDate(Calendar calendar)
{
if(calendar!=null)
calendar_date = calendar;
}
public void setDate(Date date)
{
if(date!=null)
{
calendar_date = Calendar.getInstance();
calendar_date.setTime(date);
}
}
public void setDate(int year,int month,int day)
{
if( month<12 && month>=0 && day<32 && day>=0 && year>100 && year<3000)
{
calendar_date = Calendar.getInstance();
calendar_date.set(year, month, day);
}
}
public void setTimeIn24HourFormat(int hourIn24Format,int minute)
{
if(hourIn24Format<24 && hourIn24Format>=0 && minute>=0 && minute<60)
{
if(calendar_date==null)
calendar_date = Calendar.getInstance();
calendar_date.set(calendar_date.get(Calendar.YEAR),calendar_date.get(Calendar.MONTH), calendar_date.get(Calendar.DAY_OF_MONTH),hourIn24Format, minute);
is24HourView = true;
}
}
public void setTimeIn12HourFormat(int hourIn12Format,int minute,boolean isAM)
{
if(hourIn12Format<13 && hourIn12Format>0 && minute>=0 && minute<60)
{
if(hourIn12Format==12)
hourIn12Format = 0;
int hourIn24Format = hourIn12Format;
if(!isAM)
hourIn24Format += 12;
if(calendar_date==null)
calendar_date = Calendar.getInstance();
calendar_date.set(calendar_date.get(Calendar.YEAR),calendar_date.get(Calendar.MONTH), calendar_date.get(Calendar.DAY_OF_MONTH),hourIn24Format,minute);
is24HourView = false;
}
}
public void set24HourFormat(boolean is24HourFormat)
{
is24HourView = is24HourFormat;
}
public interface ICustomDateTimeListener
{
public void onSet(Calendar calendarSelected,Date dateSelected,int year,String monthFullName,String monthShortName,int monthNumber,int date,String weekDayFullName,String weekDayShortName);
public void onCancel();
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case SET_DATE: btn_setTime.setEnabled(true);
btn_setDate.setEnabled(false);
viewSwitcher.showNext();
break;
/* case SET_TIME: btn_setTime.setEnabled(false);
btn_setDate.setEnabled(true);
viewSwitcher.showPrevious();
break;
*/
case SET: if(dialog.isShowing())
dialog.dismiss();
if(iCustomDateTimeListener!=null)
{
int month = datePicker.getMonth();
int year = datePicker.getYear();
int day = datePicker.getDayOfMonth();
calendar_date.set(year, month, day);
iCustomDateTimeListener.onSet(calendar_date, calendar_date.getTime(), calendar_date.get(Calendar.YEAR), getMonthFullName(calendar_date.get(Calendar.MONTH)), getMonthShortName(calendar_date.get(Calendar.MONTH)),calendar_date.get(Calendar.MONTH), calendar_date.get(Calendar.DAY_OF_MONTH),getWeekDayFullName(calendar_date.get(Calendar.DAY_OF_WEEK)),getWeekDayShortName(calendar_date.get(Calendar.DAY_OF_WEEK)));
}
// resetData();
break;
case CANCEL: if(dialog.isShowing())
dialog.dismiss();
if(iCustomDateTimeListener!=null)
iCustomDateTimeListener.onCancel();
resetData();
break;
}
}
/**
* @param date date in String
* @param fromFormat format of your <b>date</b> eg: if your date is 2011-07-07 09:09:09 then your format will be <b>yyyy-MM-dd hh:mm:ss</b>
* @param toFormat format to which you want to convert your <b>date</b> eg: if required format is 31 July 2011 then the toFormat should be <b>d MMMM yyyy</b>
* @return formatted date
*/
public static String convertDate(String date,String fromFormat,String toFormat)
{
String formattedDate="";
try
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(fromFormat);
Date d = simpleDateFormat.parse(date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(d);
simpleDateFormat = new SimpleDateFormat(toFormat);
simpleDateFormat.setCalendar(calendar);
formattedDate = simpleDateFormat.format(calendar.getTime());
} catch (Exception e)
{
if(e!=null)
e.printStackTrace();
}
return formattedDate;
}
/**
* @param monthNumber Month Number starts with 0. For <b>January</b> it is <b>0</b> and for <b>December</b> it is <b>11</b>.
* @return
*/
private String getMonthFullName(int monthNumber)
{
String monthName="";
if(monthNumber>=0 && monthNumber<12)
try
{
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, monthNumber);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMM");
simpleDateFormat.setCalendar(calendar);
monthName = simpleDateFormat.format(calendar.getTime());
} catch (Exception e)
{
if(e!=null)
e.printStackTrace();
}
return monthName;
}
/**
* @param monthNumber Month Number starts with 0. For <b>January</b> it is <b>0</b> and for <b>December</b> it is <b>11</b>.
* @return
*/
private String getMonthShortName(int monthNumber)
{
String monthName="";
if(monthNumber>=0 && monthNumber<12)
try
{
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, monthNumber);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM");
simpleDateFormat.setCalendar(calendar);
monthName = simpleDateFormat.format(calendar.getTime());
}
catch (Exception e)
{
if(e!=null)
e.printStackTrace();
}
return monthName;
}
/**
* @param weekDayNumber Week Number starts with 1. For <b>Sunday</b> it is <b>1</b> and for <b>Saturday</b> it is <b>7</b>.
* @return
*/
private String getWeekDayFullName(int weekDayNumber)
{
String weekName = "";
if(weekDayNumber>0 && weekDayNumber<8)
{
try {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, weekDayNumber);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE");
simpleDateFormat.setCalendar(calendar);
weekName = simpleDateFormat.format(calendar.getTime());
} catch (Exception e)
{
if(e!=null)
e.printStackTrace();
}
}
return weekName;
}
/**
* @param weekDayNumber Week Number starts with 1. For <b>Sunday</b> it is <b>1</b> and for <b>Saturday</b> it is <b>7</b>.
* @return
*/
private String getWeekDayShortName(int weekDayNumber)
{
String weekName = "";
if(weekDayNumber>0 && weekDayNumber<8)
{
try {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, weekDayNumber);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EE");
simpleDateFormat.setCalendar(calendar);
weekName = simpleDateFormat.format(calendar.getTime());
} catch (Exception e) {
if(e!=null)
e.printStackTrace();
}
}
return weekName;
}
private int getHourIn12Format(int hour24)
{
int hourIn12Format = 0;
if(hour24==0)
hourIn12Format = 12;
else if(hour24<=12)
hourIn12Format = hour24;
else
hourIn12Format = hour24-12;
return hourIn12Format;
}
private String getAMPM(Calendar calendar)
{
String ampm = (calendar.get(Calendar.AM_PM)==(Calendar.AM))? "AM":"PM";
return ampm;
}
private void resetData()
{
calendar_date = null;
is24HourView = true;
}
public static String pad(int i)
{
return i<=9?"0"+i:""+i;
}
public static String getSecondsFromMillis(long milliseconds) {
return "" + ((int) ((milliseconds / 1000) % 60));
}
public static String getMinutesFromMillis(long milliseconds) {
return "" + (int) ((milliseconds / (1000 * 60)) % 60);
}
public static String getHoursFromMillis(long milliseconds) {
return "" + (int) ((milliseconds / (1000 * 60 * 60)) % 24);
}
/**
* @param monthNumber Month Number starts with 0. For <b>January</b> it is <b>0</b> and for <b>December</b> it is <b>11</b>.
* @param year
* @return
*/
public static int getDaysInMonth(int monthNumber,int year)
{
int days=0;
if(monthNumber>=0 && monthNumber<12){
try
{
Calendar calendar = Calendar.getInstance();
int date = 1;
calendar.set(year, monthNumber, date);
days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
} catch (Exception e)
{
if(e!=null)
e.printStackTrace();
}
}
return days;
}
/**
* @param monthNumber Month Number starts with 0. For <b>January</b> it is <b>0</b> and for <b>December</b> it is <b>11</b>.
* @return
*/
public static int getDaysInMonthInPresentYear(int monthNumber)
{
int days=0;
if(monthNumber>=0 && monthNumber<12){
try
{
Calendar calendar = Calendar.getInstance();
int date = 1;
int year = calendar.get(Calendar.YEAR);
calendar.set(year, monthNumber, date);
days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
} catch (Exception e)
{
if(e!=null)
e.printStackTrace();
}
}
return days;
}
public static int getDaysDifference(Date fromDate,Date toDate)
{
if(fromDate==null||toDate==null)
return 0;
return (int)( (toDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24));
}
public static int getDaysDifference(Calendar calendar1,Calendar calendar2)
{
if(calendar1==null||calendar2==null)
return 0;
return (int)( (calendar2.getTimeInMillis() - calendar1.getTimeInMillis()) / (1000 * 60 * 60 * 24));
}
}
I am calling it in my MainActivity.Class as follows:
public class MainActivity extends Activity implements ICustomDateTimeListener
{
private TextView textView;
private DateTimePicker dateTimePicker;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textview);
dateTimePicker = new DateTimePicker(MainActivity.this, this);
dateTimePicker.set24HourFormat(true);
dateTimePicker.showDialog();
//rrr
}
@Override
public void onSet(Calendar calendarSelected, Date dateSelected, int year,
String monthFullName, String monthShortName, int monthNumber,
int date, String weekDayFullName, String weekDayShortName)
{
textView.setText(dateSelected.toLocaleString());
}
@Override
public void onCancel()
{
Log.d("datetimepickerdialog", "canceled");
}
解决方案
You must do this in onSet method of your activity:
SimpleDateFormat formatter = new SimpleDateFormat("EEEE dd MMM yyyy");
textView.setText(formatter.format(dateSelected));
这篇关于Android的日期选取器格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文