本文介绍了一天字符串格式与天furure名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要每天每周的名字,但依赖Ø实际日期和日期的未来。比如我今天需要一天的名称,然后将其命名一天,明天,日期等2天等我使这个code,但没有工作好。那么,有没有更好的办法我怎么能在android系统得到一天的名字吗?

 公共字符串getDay3Name(){
        台历挂历= Calendar.getInstance();
        calendar.setTime(新的Date());
        的String []天=新的String [] {SUNDAY,SUNDAY,SUNDAY,星期一,星期二,星期三,星期四,星期五,星期六};
        字符串天=天[calendar.get(Calendar.DAY_OF_WEEK)+1];
        回天;
    }
    公共字符串getDay4Name(){
        台历挂历= Calendar.getInstance();
        calendar.setTime(新的Date());
        的String []天=新的String [] {SUNDAY,SUNDAY,SUNDAY,星期一,星期二,星期三,星期四,星期五,星期六};
        字符串天=天[calendar.get(Calendar.DAY_OF_WEEK)+2];
        回天;
    }


解决方案

 进口java.util.Calendar中;
进口java.util.Locale中;
公共类CalSample {    私有静态最终诠释TODAY = 0;
    私有静态最终诠释TOMORROW = 1;
    私有静态最终诠释DAY_AFTER_TOMORROW = 2;
    私有静态最终诠释THREE_DAYS_FROM_TODAY = 3;
    私有静态最终诠释FOUR_DAYS_FROM_TODAY = 4;
    私有静态最终诠释FIVE_DAYS_FROM_TODAY = 5;
    私有静态最终诠释SIX_DAYS_FROM_TODAY = 6;    公共静态无效的主要(字符串ARGS []){
        CalSample calSample =新CalSample();
        calSample.getNameOfDayOfWeek(今天);
        calSample.getNameOfDayOfWeek(明天);
        calSample.getNameOfDayOfWeek(DAY_AFTER_TOMORROW);
        calSample.getNameOfDayOfWeek(THREE_DAYS_FROM_TODAY);
        calSample.getNameOfDayOfWeek(FOUR_DAYS_FROM_TODAY);
        calSample.getNameOfDayOfWeek(SIX_DAYS_FROM_TODAY);    }
    公共字符串getNameOfDayOfWeek(INT noOfDaysFromToday){
        台历挂历= Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR,noOfDaysFromToday);
        字符串名称= calendar.getDisplayName(Calendar.DAY_OF_WEEK,Calendar.LONG,Locale.ENGLISH);
        的System.out.println(日周: - +姓名);
        返回名称;
    }
}

I need name of day in week but dependent o actual date and date to future. For example I need name of day today, then name of day tomorrow, name of date +2 days etc I make this code but not working good. So is there any better way how I can in android get name of day?

 public String getDay3Name() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        String[] days = new String[] { "SUNDAY","SUNDAY","SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" };
        String day = days[calendar.get(Calendar.DAY_OF_WEEK)+1];
        return day;
    }
    public String getDay4Name() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        String[] days = new String[] { "SUNDAY","SUNDAY","SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" };
        String day = days[calendar.get(Calendar.DAY_OF_WEEK)+2];
        return day;
    }
解决方案
import java.util.Calendar;
import java.util.Locale;


public class CalSample {

    private static final int TODAY = 0;
    private static final int TOMORROW = 1;
    private static final int DAY_AFTER_TOMORROW = 2;
    private static final int THREE_DAYS_FROM_TODAY = 3;
    private static final int FOUR_DAYS_FROM_TODAY = 4;
    private static final int FIVE_DAYS_FROM_TODAY = 5;
    private static final int SIX_DAYS_FROM_TODAY = 6;

    public static void main(String args[]){
        CalSample calSample = new CalSample();
        calSample.getNameOfDayOfWeek(TODAY);
        calSample.getNameOfDayOfWeek(TOMORROW);
        calSample.getNameOfDayOfWeek(DAY_AFTER_TOMORROW);
        calSample.getNameOfDayOfWeek(THREE_DAYS_FROM_TODAY);
        calSample.getNameOfDayOfWeek(FOUR_DAYS_FROM_TODAY);
        calSample.getNameOfDayOfWeek(SIX_DAYS_FROM_TODAY);

    }
    public String getNameOfDayOfWeek(int noOfDaysFromToday){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, noOfDaysFromToday);
        String name = calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.ENGLISH);
        System.out.println("Day Of Week:- " + name);
        return name;
    }
}

这篇关于一天字符串格式与天furure名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 02:59