Getting Year, Month and Date in Android - android

I am trying to get today's Year, Month and Date using following code;
Calendar calendar = Calendar.getInstance();
int thisYear = calendar.get(Calendar.YEAR);
Log.d(TAG, "# thisYear : " + thisYear);
int thisMonth = calendar.get(Calendar.MONTH);
Log.d(TAG, "# thisMonth : " + thisMonth);
int thisDay = calendar.get(Calendar.DAY_OF_MONTH);
Log.d(TAG, "$ thisDay : " + thisDay);
But it gives "2012 for year 1 for month and 28 for date" which is not today's date. What I have done wrong?

Would I be correct in assuming this is running on a Emulator? If so, Set the emulator date correctly, and it should be correct.
From memory, that code should do what you expect.

Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
calendar.get(Calendar.YEAR)

import java.util.Calendar;
import java.util.TimeZone;
import android.widget.Toast;
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
int currentYear = calendar.get(Calendar.YEAR);
int currentMonth = calendar.get(Calendar.MONTH) + 1;
int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
Toast.makeText(this,"Today's Date: " + currentYear + currentMonth + currentDay, Toast.LENGTH_SHORT).show();
"TimeZone" will work great if your application targets for Android API 27 platform or above

The month starts from zero so you have to add 1 with the given month to show the month number we are familiar with.
int thisMonth = calendar.get(Calendar.MONTH);
Log.d(TAG, "# thisMonth : " + (thisMonth+1));
This will show you the current month starting with 1.

try this one..
Calendar instance = Calendar.getInstance();
currentMonth = instance.get(Calendar.MONTH);
currentYear = instance.get(Calendar.YEAR);
int month=currentMonth+1;

I tried your code and it is giving correct output. You should try checking time in your emulator/phone on which you are trying this code.
According to getInstance docs, it sets to current date and time by Default.

Try to use:
Date dt = new Date();
dt.getYear();
dt.getMonth();
dt.getDay();
and see if you get the same result.
If so, your system date is probably out of sync.
Check the Date class documentation for more details:

This gives the time and date of your android system so first check it.

You can use SimpleDateFormat
Initialising as #SuppressLint("SimpleDateFormat") final SimpleDateFormat dateAndTime = new SimpleDateFormat("DD-MM-yy", Locale.getDefault());
and String timeStamp = dateAndTime.format(new Date());

Related

get start and end date of the week

Actually i want to display a set of data with the start and end date of the week were that particular date falls on. In my emulator its working fine. Eg. If i give Apr 23 its giving me start date of the week as 22 Apr and end date as 28 Apr, but if i try to build the same code in my device its showing start date of the week as 27 Apr and end date as 28 Apr.
Piece of Code which i am using:
//to get first day of week
cal1.set(Calendar.DAY_OF_WEEK, 1);
int day1 = cal1.get(Calendar.DAY_OF_MONTH);
//to get last day of week
cal1.set(Calendar.DAY_OF_WEEK, 7);
int day7 = cal1.get(Calendar.DAY_OF_MONTH);
I don't know why you getting that data but this is how I get the first and last date, take look might help. (its written to give current week's first and last date, so might have to tweak it little bit.)
Calendar calendar = Calendar.getInstance();
Date date1 = calendar.getTime();
//current date to check that our week lies in same month or not
SimpleDateFormat checkformate = new SimpleDateFormat("MM/yyyy");
String currentCheckdate= checkformate.format(date1);
int weekn = calendar.get(Calendar.WEEK_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
//resat calender without date
calendar.clear();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.set(Calendar.WEEK_OF_MONTH,weekn);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.YEAR,year);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date datef = calendar.getTime();
//move date to 6 days + to get last date of week
Long timeSixDayPlus = calendar.getTimeInMillis()+518400000L;
Date dateL = new Date(timeSixDayPlus);
String firtdate = simpleDateFormat.format(datef);
String lastdate = simpleDateFormat.format(dateL);
String firtdateCheck = checkformate.format(datef);
String lastdateCheck = checkformate.format(dateL);
//if our week lies in two different months then we show only current month week part only
if (!firtdateCheck.toString().equalsIgnoreCase(currentCheckdate)) {
firtdate = "1" + "/" + calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.YEAR);
}
if (!lastdateCheck.toString().equalsIgnoreCase(currentCheckdate)) {
int ma = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
lastdate = String.valueOf(ma) + "/" + calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.YEAR);
}
Log.e("current","=>>"+firtdate+" to "+lastdate);
To get the first day of the week -
initialize the Calendar as per your need. in this case, I am getting the current date calendar.
set the current day of the week to the first day of the week.
get the corresponding date.
Calendar calendar = Calendar.getInstance();
//optional step
calendar.setFirstDayOfWeek(Calendar.SUNDAY);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
int firstDateOfWeek = calendar.get(Calendar.DATE);
To get the last date of the week -
follow the step as above to initialize.
set the day of the week as Saturday.
get the corresponding date.
Calendar calendar = Calendar.getInstance();
//optional step
calendar.setFirstDayOfWeek(Calendar.SUNDAY);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
int lastDateOfWeek = calendar.get(Calendar.DATE);
In this way, you can get eh first and the last date for the week. one thing to keep in mind that I have set the first day of the week as SUNDAY. set as per your need. although it is purely optional to set the first day of the week. this gives you a more transparency in code.

Android on Gregorian Calendar

Guys I need help i want to display the month and date along with the day of following 7 days in format like
Sat - 22
Sun - 23
Mon - 24
..
Using a loop
what should be my approach within the loop ::
Calendar cal = new GregorianCalendar();
for(i = 0;i<7;i++)
{
int month = cal.get(Calendar.MONTH);
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH + i); // Giving error here
day[i] = dayOfMonth + month + "";
}
I am totally a beginner in this field and im confused help me out
Changing:
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH + i);
to:
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH) + i;
Will fix the error.
But what you are trying to do can be better accomplished with:
Calendar cal = Calendar.getInstance();
for(i = 0; i < 7; i++) {
cal.add(Calendar.DAY_OF_MONTH, 1); // Adds a day to the date and takes care
// of adding month when its the last day of the month already
int weekDay = cal.get(Calendar.DAY_OF_WEEK); // Get weekday name like Sunday
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH)
day[i] = weekDay + " - " + dayOfMonth;
}

How to get current hour in android?

Time class is no longer possible to use.
I want to ask you, how to detect in app 3-4am?
I need that to set up for example night mode in my app.
Can you give me some example how to do it?
Instead of using Time (because Time class was deprecated in API level 22.) you can use Calendar for getting current hour
val rightNow = Calendar.getInstance()
val currentHourIn24Format: Int =rightNow.get(Calendar.HOUR_OF_DAY) // return the hour in 24 hrs format (ranging from 0-23)
val currentHourIn12Format: Int = rightNow.get(Calendar.HOUR) // return the hour in 12 hrs format (ranging from 0-11)
We can use the Calendar class to get a format like "HH:mm:ss"
Calendar calendar = Calendar.getInstance();
int hour24hrs = calendar.get(Calendar.HOUR_OF_DAY);
int hour12hrs = calendar.get(Calendar.HOUR);
int minutes = calendar.get(Calendar.MINUTE);
int seconds = calendar.get(Calendar.SECOND);
System.out.println("Current hour 24hrs format: " + hour24hrs + ":" + minutes +":"+ seconds);
System.out.println("Current hour 12hrs format: " + hour12hrs + ":" + minutes +":"+ seconds);
Other option using the Date class and applying the format "HH:mm:ss":
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
String dateformatted = dateFormat.format(date);
System.out.println(dateformatted);
You can use following methods:
SimpleDateFormat format = new SimpleDateFormat("HH", Locale.US);
String hour = format.format(new Date());
Calendar calendar = Calendar.getInstance();
int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);

How to get week number. (I have date)?

I'm currently working on a project that needs to find the week number of a given date.
Can you give me a code snippet for my problem?
Thanks.
You can try this
Calendar now = Calendar.getInstance();
now.set(Calendar.YEAR,2013);
now.set(Calendar.MONTH,04);//0- january ..4-May
now.set(Calendar.DATE, 04);
System.out.println("Current week of month is : " +
now.get(Calendar.WEEK_OF_MONTH));
System.out.println("Current week of year is : " +
now.get(Calendar.WEEK_OF_YEAR));
This should solve your problem with using this:
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int weekOfYear = ca.get(Calendar.WEEK_OF_YEAR);

save current date into database

I want to save system date on my database, but it returns : 22/0/2013.
here my code :
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month_ = cal.get(Calendar.MONTH);
int day_ = cal.get(Calendar.DATE);
String FullDate = (""+day_+"/"+month_+"/"+year);
String text_Rate=(String.valueOf(FullDate));
Log.d("System Date show", text_Rate);
where i'm doing wrong.
This is the correct behavior. The Java Calendar month is 0-based (January is 0, December is 11).
If you really want to store it as 22/1/2013, simply add +1 to your month:
int month_ = cal.get(Calendar.MONTH) + 1;
you should save dates as long to sqlite. that is easier and less prone to errors. see here for calendar to long.

Categories

Resources