如何以特定格式查找输入表单用户日期和返回日期的具体日期

时间:2013-08-27 10:21:00

标签: android

我想显示特定日期的日期,我的日期是字符串,completedate2="2013/08/19" 如何更改此功能以从用户特定日期和该日期的返回日期获取输入。

如何使这个功能动态只是为了输入和返回日,有没有办法做到这一点?

         calculate date (String date)
         Calendar calendar=Calendar.getInstance();
         calendar.setTime(specific_date);
         int weekday = calendar.get(Calendar.DAY_OF_WEEK);
         return day;










     String  game  =     getdayofweek(completedate2);


   private String getdayofweek(String week1) {



     String dtStart = week1; 

        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");  
        try {  
            date1 = format.parse(dtStart);

        } catch (ParseException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }

         calender1 = new GregorianCalendar();

        calender1.setTime(date1);

        String[] days = new String[] {"", "SUNDAY", "MONDAY", "TUESDAY", 
       "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" };


        String day = days[calender1.get(Calendar.DAY_OF_WEEK)];

//    String wekdays ;
    //  wekdays = day;
        return day;


    }

2 个答案:

答案 0 :(得分:1)

public static String getDayOfWeek(String stringDate) {
    String dayOfWeek;

    SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
    Date date = null;
    Calendar calender = new GregorianCalendar();

    try {
        date = format.parse(stringDate);
    } catch (ParseException e) {
        e.printStackTrace();
        dayOfWeek = null;
    }

    calender.setTime(date);

    switch (calender.get(Calendar.DAY_OF_WEEK)) {
    case Calendar.MONDAY:
        dayOfWeek = "Monday";
        break;
    case Calendar.TUESDAY:
        dayOfWeek = "Tuesday";
        break;
    case Calendar.WEDNESDAY:
        dayOfWeek = "Wednesday";
        break;
    case Calendar.THURSDAY:
        dayOfWeek = "Thursday";
        break;
    case Calendar.FRIDAY:
        dayOfWeek = "Friday";
        break;
    case Calendar.SATURDAY:
        dayOfWeek = "Saturday";
        break;
    case Calendar.SUNDAY:
        dayOfWeek = "Sunday";
        break;

    default:
        dayOfWeek = null;
        break;
    }

    return dayOfWeek;
}

答案 1 :(得分:0)

使用此功能将日期解析为您所需的格式

public static Date parseDate(String date, String formate) {
    SimpleDateFormat formatter = new SimpleDateFormat(formate);
    try {
        return formatter.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
        return new Date();
    }
}

然后使用此功能从日期获取日期

public static int getDayOfTheYear(String date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setFirstDayOfWeek(Calendar.SUNDAY);
    calendar.setTime(Utils.parseDate(date));
    int day = calendar.get(Calendar.DAY_OF_YEAR) ;

    return day;
}

public static Date parseDate(String date) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    try {
        return formatter.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
        return new Date();
    }
}