I have been posting about various libraries and concepts that Android and other mobile technologies are using for applications. But, I have learn that we should never forget the basics and always understand it perfectly.
In this post, I will show how to work with java.util.Date, java.util.Calendar and
java.text.SimpleDateFormat.
The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week. An instant in time can be represented by a millisecond value that is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian).
SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization.
Refer the below link for complete sample code:-
Download Sample Code
Have a look on few code snippets,
//activity_main.xml
//rowitem_calender.xml
//CalenderListAdapter.java
//MainActivity.java
In this post, I will show how to work with java.util.Date, java.util.Calendar and
java.text.SimpleDateFormat.
The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week. An instant in time can be represented by a millisecond value that is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian).
SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization.
Refer the below link for complete sample code:-
Download Sample Code
Have a look on few code snippets,
//activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:scrollbars="none" android:divider="@android:color/white" android:layout_height="match_parent" /> </LinearLayout> |
//rowitem_calender.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/tv_date" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff9933" android:gravity="center" android:padding="5dp" android:textColor="@android:color/black" android:textStyle="bold" /> <TextView android:id="@+id/tv_day" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white" android:gravity="center" android:padding="5dp" android:textColor="@android:color/black" android:textStyle="bold" /> <TextView android:id="@+id/tv_year" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#138808" android:gravity="center" android:padding="5dp" android:textColor="@android:color/black" android:textStyle="bold" /> </LinearLayout> |
//CalenderListAdapter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | import android.content.Context; import android.graphics.Color; import android.support.v4.util.ArrayMap; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.harshalbenake.calenderdates.R; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; public class CalenderListAdapter extends BaseAdapter { private Context mContext; private String[] mDateArray,mDayArray,mYearArray; public CalenderListAdapter(Context context) { this.mContext = context; Calendar calendar = Calendar.getInstance(); SimpleDateFormat simpleDateFormatDate = new SimpleDateFormat("dd"); String[] dateArray = new String[365]; for (int i = 0; i < 365; i++) { Date date = calendar.getTime (); // now format it using SimpleDateFormat String format = simpleDateFormatDate.format (date); dateArray[i] = format; calendar.add (Calendar.DAY_OF_WEEK, 1); } SimpleDateFormat simpleDateFormatDay = new SimpleDateFormat("MMM"); String[] dayArray = new String[365]; for (int i = 0; i < 365; i++) { Date date = calendar.getTime (); // now format it using SimpleDateFormat String format = simpleDateFormatDay.format (date); dayArray[i] = format; calendar.add (Calendar.DAY_OF_WEEK, 1); } SimpleDateFormat simpleDateFormatYear = new SimpleDateFormat("yyyy"); String[] yearArray = new String[365]; for (int i = 0; i < 365; i++) { Date date = calendar.getTime (); // now format it using SimpleDateFormat String format = simpleDateFormatYear.format (date); yearArray[i] = format; calendar.add (Calendar.DAY_OF_WEEK, 1); } this.mDateArray = dateArray; this.mDayArray = dayArray; this.mYearArray = yearArray; } @Override public int getCount() { return mDateArray.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolderItem viewHolder; if (convertView == null) { // inflate the layout LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.rowitem_calender, parent, false); viewHolder = new ViewHolderItem(); viewHolder.tv_date = (TextView) convertView.findViewById(R.id.tv_date); viewHolder.tv_day = (TextView) convertView.findViewById(R.id.tv_day); viewHolder.tv_year = (TextView) convertView.findViewById(R.id.tv_year); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolderItem) convertView.getTag(); } viewHolder.tv_date.setText(mDateArray[position]); viewHolder.tv_day.setText(mDayArray[position]); viewHolder.tv_year.setText(mYearArray[position]); return convertView; } private static class ViewHolderItem { TextView tv_date,tv_day,tv_year; } } |
//MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package com.harshalbenake.calenderdates; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ListView; import com.harshalbenake.calenderdates.adapter.CalenderListAdapter; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listview=(ListView)findViewById(R.id.listview); CalenderListAdapter arrayAdapter = new CalenderListAdapter(MainActivity.this); listview.setAdapter(arrayAdapter); } } |
No comments:
Post a Comment