【问题标题】:Display dates in multiple fragments在多个片段中显示日期
【发布时间】:2014-03-23 11:22:12
【问题描述】:

我在想办法以片段形式显示日期时遇到了麻烦。我需要做的是在片段顶部显示今天的日期,然后当用户滑动或按下按钮时,片段将替换前一个片段并显示明天的日期,然后下一个片段将显示明天之后的日期等等上。

【问题讨论】:

  • 我了解如何使用 GregorianCalendar 获取明天的日期,但我如何获得后天的日期?

标签: java android date fragment


【解决方案1】:

我不确定我是否理解问题所在,但您可以像这样获取明天的日期或相对于当前时间的任何日期:

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, numberOfDaysToAdd);
Date date = calendar.getTime();

【讨论】:

    【解决方案2】:

    乔达时间

    您可以在 Android 中使用Joda-Time 库。

    如果您只需要日期,没有任何时间或时区,请使用 LocalDate 类。

    示例代码

    LocalDate today = LocalDate.now();
    LocalDate tomorrow = today.plusDays( 1 );
    LocalDate dayAfterTomorrow = tomorrow.plusDays( 1 ); // or today.plusDays( 2 );
    

    格式化

    要将 LocalDate 格式化为字符串,请参阅 this answer

    【讨论】:

      【解决方案3】:

      创建一个包含当前日期的Fragment 和一个包含偏移量的Bundle extra。

      public class DateFragment extends Fragment {
      
          private static final String DAY_OF_YEAR = "datefragment.DAY_OF_YEAR";
      
          /**
           * Empty constructor as per the {@link Fragment} docs
           */
          public DateFragment() {
          }
      
          /**
           * @param offset The number of days to off the {@link Calendar}
           * @return A new instance of <code>DateFragment</code>
           */
          public static DateFragment newInstance(int offset) {
              final Bundle args = new Bundle();
              args.putInt(DAY_OF_YEAR, offset);
              final DateFragment fragment = new DateFragment();
              fragment.setArguments(args);
              return fragment;
          }
      
          /**
           * {@inheritDoc}
           */
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      
              final SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault());
              final Calendar c = Calendar.getInstance();
              c.add(Calendar.DAY_OF_YEAR, getArguments().getInt(DAY_OF_YEAR, 1));
      
              final TextView text = new TextView(getActivity());
              text.setText(df.format(c.getTime()));
              return text;
          }
      
      }
      

      在你的Activity

          // Your custom PagerAdapter
          final PagerAdapter adapter = new PagerAdapter(getFragmentManager());
          for (int i = 0; i < 365; i++) {
              adapter.buildData(DateFragment.newInstance(i));
          }
          // Bind the data to the ViewPager
          final ViewPager pager = (ViewPager) findViewById(R.id.pager);
          pager.setAdapter(adapter);
          pager.setOffscreenPageLimit(adapter.getCount());
      

      结果

      这将为您提供类似于“2014 年 3 月 23 日”的信息,直到您选择时,在这种情况下为一年后。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-08
        • 1970-01-01
        • 1970-01-01
        • 2019-07-31
        • 1970-01-01
        相关资源
        最近更新 更多