android


get day name from day number of the year android


I want to get day name of the week from the day number of the year in android. Example: what would be the day name of 344th day of year 2016. My server gives me data like this & have to use this to find out the day name.
{
"id": 106,
"month": 12,
"year": 2016,
"yDay": 344,
"hour": 19
}
I can get the current day number from the calendar.
Calendar calendar = Calendar.getInstance();
int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
One way how you could archieve it by doing something like this:
public String getNameOfDay(int year, int dayOfYear) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.DAY_OF_YEAR, dayOfYear);
String days[] = {"Sunday", "Monday", "Tuesday", "Thursday", "Friday", "Saturday"};
int dayIndex = calendar.get(Calendar.DAY_OF_WEEK);
return days[dayIndex - 1];
}
Firstly, you need to init a Calendar instance with the year and day of year:
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_YEAR, yourDayOfYear);
c.set(Calendar.YEAR, yourYear);
Then you can get the day of week from Calendar
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
You can get dayOfWeek from date (form date from yDay, month, year)
Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
What is the easiest way to get the current day of the week in Android?
so you will get the current day and same way you will get current date and in th esame way number of days for same date . just calculate now
Try this it gives you the name according to your year and month
// Assuming that you already have this.
int year = 2016;
int month = 12;
int day = 2;
// First convert to Date. This is one of the many ways.
String dateString = String.format("%d-%d-%d", year, month, day);
Date date = new SimpleDateFormat("yyyy-M-d").parse(dateString);
// Then get the day of week from the Date based on specific locale.
String dayOfWeek = new SimpleDateFormat("EEEE", Locale.ENGLISH).format(date);
System.out.println(dayOfWeek); // Friday
if you have format of date then you can try this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dayName("2016-12-02 00:00:00", "YYYY-MM-DD HH:MM:ss")
}
public static String dayName(String inputDate, String format){
Date date = null;
try {
date = new SimpleDateFormat(format).parse(inputDate);
} catch (ParseException e) {
e.printStackTrace();
}
return new SimpleDateFormat("EEEE", Locale.ENGLISH).format(date);
}
Another way of accomplishing this would be using the Joda Time Package.
Android currently only runs on Java 1.7 and thus does not have good time features by itself.
Add Joda Time to your build.gradle:
compile 'joda-time:joda-time:2.9.4'
and then you can do something similar to this:
public String getNameOfDay(int year, int yDay) {
String pattern = year + ";" + yDay;
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("y;D");
DateTime requestedDate = DateTime.parse(pattern, dateTimeFormatter);
int dayOfWeek = requestedDate.getDayOfWeek();
//Notice that Monday is 1 and Sunday is 7
String[] days = {"Monday", "Tuesday", "Thursday", "Friday", "Saturday", "Sunday"};
return days[dayOfWeek - 1];
}
Please notice, that I am only offering an alternative to the already posted ways of accomplishing this goal. If you don't want to add joda time to your project you can just go with the ways what the others have already said.
You can get the day of the week as a String directly from the Calendar object:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_YEAR, dayOfYear); // Set the day of the year value
String day = cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
// Use Calendar.SHORT for abbreviated names or .LONG for full names
// and be sure to change the locale as necessary

Related Links

Android Logging set verbose for all tags (logcat)
Unfortunately, <App name> has stopped working. Android?Eclipse application
Generating new objects from findViewById() method
Android: view local html file not showing all browsers
how to show tweets in list view in android?
AirPush android: not working on tablets
Storing files on SD card/internal storage
Display Maps with multiple locations
Android - how to set position of children view in onLayout
Display a textview below a ImageView in a Gridview
Change text color of selected item of Listview in Android
Updating time in AlarmManager
Keep the scroll position of a list while adding item to the top in Android
DialogFragment animation of layout and “Attempting to destroy the window while drawing!” error
Error occur in FragmentStatePagerAdapter?
Remove floor plans from Google Maps Android API v2

Categories

HOME
fabricjs
delphi-7
fpga
pda
powerpoint
static-site
spring-data-mongodb
echo
google-admin-sdk
nfs
rcloud
g++
little-proxy
commonjs
gson
distribution
php-5.6
screen-readers
glyphicons
dreamweaver
google-data-studio
asp.net-mvc-viewmodel
foreign-keys
mapstruct
solrcloud
google-maps-autocomplete
postscript
jasmine-node
tpm
ceylon
openmdao
userdefaults
testbed
deltaspike
angularjs-material
linkedhashmap
getpixel
sql-delete
batching
sql-server-ce
playframework-2.1
railstutorial.org
setup.py
mcustomscrollbar
skylink
datacachefactory
oracle-service-bus
openrasta
fclose
dbcontext
tinymce-3
yii2-user
subnet
cup
vulcanize
i386
appscale
shopping
tracker-enabled-dbcontext
cortex-m
grgit
computer-architecture
oracle-spatial
rate-limiting
toolkit
iced-coffeescript
typemock
liquid-layout
ms-access-2000
ssis-data-flow
punycode
teaspoon
paw
jquery-slider
buffering
renderer
synapse
getopt
multitasking
ice-cube
nevron
kaleidoscope
actiondispatch
cardreader
veracity
correlated-subquery
paginator
htdocs
android-holo-everywhere
rmdir
cuteeditor
mtu
ie-compatibility-mode
sifr
filesystemobject
skype4java
wordprocessingml
firefox-3
commercial-application

Resources

Encrypt Message