我在我的应用程序中使用自定义的号码选择器,以获取月份和年份作为用户的输入。通过在数字选择器中选择月份,月份值应另存为字符串,年份应另存为整数值。所以我只想从字符串月份名称和整数年份值中获取完整的日期格式。
This is my customized month picker.
final Dialog d = new Dialog ( getContext ( ) );
d.setContentView ( R.layout.dialog );
final NumberPicker monthNp = (NumberPicker) d.findViewById ( R.id.monthPicker );
final NumberPicker yearNp = (NumberPicker) d.findViewById ( R.id.yearPicker );
Button okDialog = (Button) d.findViewById ( R.id.ok_dialog );
Button cancelDialog = (Button) d.findViewById ( R.id.cancel_dialog );
monthNp.setMinValue ( 0 );
monthNp.setMaxValue ( monthArray.length - 1 );
monthNp.setDisplayedValues ( monthArray );
monthNp.setWrapSelectorWheel ( false );
monthNp.setValue ( imonth );
monthNp.setOnValueChangedListener ( new NumberPicker.OnValueChangeListener ( ) {
@Override
public void onValueChange ( NumberPicker picker, int oldVal, int newVal ) {
if ( oldVal != 0 ) {
changed = 1;
String getMonthFromNp = monthArray[newVal];
}
}
} );
}
if ( yearNp != null ) {
yearNp.setMinValue ( year - 15 );
yearNp.setMaxValue ( year );
yearNp.setValue ( year );
yearNp.setWrapSelectorWheel ( false );
yearNp.setOnValueChangedListener ( new
NumberPicker.OnValueChangeListener ( ) {
@Override
public void onValueChange ( NumberPicker picker, int oldVal,
int newVal ) {
if ( oldVal != 0 ) {
changed = 1;
int getYearFromNp = yearNp.getValue ( );
}
}
} );
try{
SimpleDateFormat monthFromNP = new SimpleDateFormat ( "MMMM" );
Date month = monthFromNP.parse( getMonthFromNp );
calendar = Calendar.getInstance ( );
calendar.setTime ( month );
calendar.set ( Calendar.YEAR, getYearFromNp );
calendar.set ( Calendar.MONTH, +1 );
calendar.set ( Calendar.DAY_OF_MONTH, 0 );
calendar.set ( Calendar.DATE,
calendar.getActualMinimum ( Calendar.DAY_OF_MONTH ) );
calendar.set ( Calendar.HOUR_OF_DAY, 0 );
calendar.set ( Calendar.MINUTE, 0 );
calendar.set ( Calendar.SECOND, 0 );
calendar.set ( Calendar.MILLISECOND, 0 );
startDate = calendar.getTime ( );
calendar.set ( Calendar.DATE,
calendar.getActualMaximum ( Calendar.DAY_OF_MONTH ) );
calendar.set ( Calendar.HOUR_OF_DAY, 23 );
calendar.set ( Calendar.MINUTE, 59 );
calendar.set ( Calendar.SECOND, 59 );
calendar.set ( Calendar.MILLISECOND, 999 );
endDate = calendar.getTime ( );
}
catch ( ParseException e ) {
e.printStackTrace ( );
}
答案 0 :(得分:1)
int monthVal = 9; // 0-based index, so 9 means October
int yearVal = 2018;
YearMonth ym = YearMonth.of(yearVal, Month.values()[monthVal]);
System.out.println("Year and month selected: " + ym);
此代码段的输出为:
选择的年月:2018-10
从您的代码看来,您需要一个包含所选月份的间隔的开始和结束。如果它对您有用,建议您设置半开间隔,即从开始日期(包括首尾)到结束日期排除。这样省去了将时间设置为23:59:59.999之类的麻烦。在这个简单的代码段中,我仅设置日期,因为这确实足以代表间隔:
LocalDate startDateInclusive = ym.atDay(1);
LocalDate endDateExclusive = ym.plusMonths(1).atDay(1);
System.out.println("From " + startDateInclusive + " inclusive to "
+ endDateExclusive + " exclusive");
从2018年10月1日起至2018年11月1日止
如果某些老式API需要老式的Date
对象,而您现在不能更改:
Instant startInstantInclusive = startDateInclusive
.atStartOfDay(ZoneId.systemDefault())
.toInstant();
Date oldfashionedDate = DateTimeUtils.toDate(startInstantInclusive);
System.out.println("As old-fashioned Date: " + oldfashionedDate);
在欧洲/哥本哈根时区的计算机上,我得到了:
老式日期:CEST 2018年10月1日星期一00:00:00
对结束日期进行类似的操作。
是的,java.time在较新和较旧的Android设备上均可正常运行。它只需要至少 Java 6 。
Date oldfashionedDate = Date.from(startInstantInclusive)
从Instant
到Date
进行协调。org.threeten.bp
导入日期和时间类。java.time
。java.time
。java.time
向Java 6和7(JSR-310的ThreeTen)的反向端口。答案 1 :(得分:0)
尝试这种方法:
int year = 2018;
String month = "October";
String str = month +" "+ year;
SimpleDateFormat sdf = new SimpleDateFormat("MMMM yyyy");
Date date = sdf.parse(str);