Change the Date Format before setText of TextView - java

I using DatePicker for to choose the date.Everything going good. But the problem is to Format the date.
// display current date
public void setCurrentDateOnView() {
tvDisplayDate = (TextView) findViewById(R.id.tvDate);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
tvDisplayDate.setText(new StringBuilder().append(year).append("-").append(month + 1).append("-").append(day).append(" "));
// Month is 0 based, just add 1
}
As per Code i am getting the OUTPUT: 2016-8-22
but
My expected output is 2016-08-22(I want month like this 08)
Thank you,
regards,
Karthi.

Use SimpleDateFormat:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
tvDisplayDate.setText(sdf.format(c.getTime()));
For more Information about SimpleDateFormat see the official documentation

Try this it may be help to you
public static String updateDate(int day, int month, int year) {
String monthformatted = "";
if (month < 10)
monthformatted = "0" + month;
else
monthformatted = String.valueOf(month);
String dayformatted = "";
if (day < 10)
dayformatted = "0" + day;
else
dayformatted = String.valueOf(day);
// Append in a StringBuilder
String aDate = new StringBuilder().append(year).append('-')
.append(monthformatted).append("-").append(dayformatted).toString();
return aDate;
}
and called this method like
tvDisplayDate.setText(updateDate(day,month+1,year));

private SimpleDateFormat dateFormatter;
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
tvDisplayDate.setText(dateFormatter.format(newDate.getTime()));
Add this and You will succeed

// display current date
public void setCurrentDateOnView() {
tvDisplayDate = (TextView) findViewById(R.id.tvDate);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
String formattedDate = String.format(Locale.ENGLISH, "%d-%02d-%02d", year, (month + 1), day);
tvDisplayDate.setText(formattedDate);
}

Use SimpleDateFormat class.. There are a lot of ways to do it..
something like this..
SimpleDateFormat sdfSource = new SimpleDateFormat("dd-MM-yy");
// you can add any format..
Date date = sdfSource.parse(strDate);

String format will help you ...
String month=String.format("%02d",cal.get(Calendar.MONTH));
String day=String.format("%02d",cal.get(Calendar.DAY_OF_MONTH)+1);
String year=String.format("%02d",cal.get(Calendar.YEAR));
String full_date = month+ "-" + day + "-" +year ;

You can append zero using String formation
String.format("%02d", month);
// display current date
public void setCurrentDateOnView() {
tvDisplayDate = (TextView) findViewById(R.id.tvDate);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
tvDisplayDate.setText(new StringBuilder().append(year).append("-").append(String.format("%02d", month+ 1)).append("-").append(day).append(" "));
// Month is 0 based, just add 1
}
Hope this will help

use the below code to set date as you want
tvDisplayDate.setText(String.format("%04d-%02d-%0d,year,month,date);

tl;dr
LocalDate.now( ZoneId.of( "America/Montreal" ) ).toString()
java.time
You are using troublesome old legacy classes now supplanted by the java.time classes.
Be sure to specify a time zone. If omitted your JVM’s current zone is implicitly applied. That default varies, even during runtime. Better to be specific.
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
Or specify the year, month, day-of-month.
LocalDate ld = LocalDate.of( 2016 , 1 , 2 );
The month number is sane, 1-12 for January-December. Alternatively you can pass a Month enum object instead of mere integer.
LocalDate ld = LocalDate.of( 2016 , Month.JANUARY , 2 );
Generate String
The java.time classes use standard ISO formats when parsing and generating strings. That happens to be your desired format.
String output = today.toString();
Same format can be parsed.
LocalDate ld = LocalDate.parse( "2016-01-02" );

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy",Locale.US);
tvDisplayDate.setText(simpleDateFormat.format(new Date().getTime()));

Related

Check my current time is exist between 7:00 PM -10.00 AM in java android

Example: my current time = 8:25 PM it means the current time is inside 7:00 PM to 10.00 AM. So how can I determined it & if inside show a message?
It's for a restaurant time restriction. from 7:00 PM to 10.00 AM time range user can't order anything.
try {
// Start Time
String string1 = "07:00 PM";
Date time1 = new SimpleDateFormat("hh:mm a").parse(string1);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(time1);
calendar1.add(Calendar.DATE, 1);
// End Time
String string2 = "10:00 AM";
Date time2 = new SimpleDateFormat("hh:mm a").parse(string2);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(time2);
calendar2.add(Calendar.DATE, 1);
// Get Current Time
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
String currenttime = sdf.format(date);
Date d = new SimpleDateFormat("hh:mm a").parse(currenttime);
Calendar calendar3 = Calendar.getInstance();
calendar3.setTime(d);
calendar3.add(Calendar.DATE, 1);
Date x = calendar3.getTime();
if (x.after(calendar1.getTime()) && x.before(calendar2.getTime())) {
System.out.println("Not possible to order now");
}
else
{
System.out.println("YES POSSIBLE");
}
} catch (ParseException e) {
e.printStackTrace();
}
Here if you want to avoid NullPointerException & ParseException checking:
public static boolean isAvailableForBooking() {
/* 10:00 AM */
final int OPEN_HOUR = 10; /* 0 - 23*/
final int OPEN_MINUTE = 0; /* 0 - 59*/
final int OPEN_SECOND = 0; /* 0 - 59*/
/* 07:00 PM */
final int CLOSED_HOUR = 19;
final int CLOSED_MINUTE = 0;
final int CLOSED_SECOND = 0;
Calendar openHour = Calendar.getInstance();
openHour.set(Calendar.HOUR_OF_DAY, OPEN_HOUR);
openHour.set(Calendar.MINUTE, OPEN_MINUTE);
openHour.set(Calendar.SECOND, OPEN_SECOND);
Calendar closedHour = Calendar.getInstance();
closedHour.set(Calendar.HOUR_OF_DAY, CLOSED_HOUR);
closedHour.set(Calendar.MINUTE, CLOSED_MINUTE);
closedHour.set(Calendar.SECOND, CLOSED_SECOND);
Calendar now = Calendar.getInstance();
return now.after(openHour) && now.before(closedHour);
}
tl;dr
Use modern java.time class, LocalTime.
( ! localTime.isBefore( LocalTime.of( 19 , 0 ) ) ) // Is not before the start… (meaning, is equal to or later than)
&& // …and…
localTime.isBefore( LocalTime.of( 7 , 0 ) ) ; // is before the end.
java.time
Never use Calendar or Date classes. These terrible classes were supplanted years ago by the modern java.time classes defined in JSR 310.
LocalTime start = LocalTime.of( 19 , 0 ) ; // 7 PM.
LocalTime end = LocalTime.of( 10 , 0 ) ; // 10 AM.
Determining the current time requires a time zone. For any given moment, the time of day, and the date, varies around the globe by zone.
If you want to use the JVM’s current default time zone, call ZoneId.systemDefault().
ZoneId z = ZoneId.of( "Africa/Casablanca" ) ;
LocalTime localTime = LocalTime.now( z ) ;
Ask if the current time is equal to or later than the start and before the end. Tip: another way to ask “is equal to or later” is “is not before”.
boolean withinTimeRange = ( ! localTime.isBefore( start ) ) && localTime.isBefore( end ) ;
For early Android before 26, add the ThreeTenABP library to your project to get most of the java.time functionality with nearly the same API.
If you tried any code then please post it otherwise, You can check it by setting your current time and your service start time and end time on a Calendar Object and then get Date object from the calendar and can compare these dates.
String SERVICE_START_TIME="202-05-04 19:00:00";
String SERVICE_END_TIME="202-05-05 10:00:00";
public static boolean isValidTime() {
try {
Date time1 = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss",
Locale.getDefault()).parse(SERVICE_START_TIME);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(time1);
calendar1.add(Calendar.DATE, 1);
Date time2 = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss",
Locale.getDefault()).parse(SERVICE_END_TIME);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(time2);
calendar2.add(Calendar.DATE, 1);
Date d = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss",
Locale.getDefault()).parse(getCurrentTime());
Calendar calendar3 = Calendar.getInstance();
calendar3.setTime(d);
calendar3.add(Calendar.DATE, 1);
Date now = calendar3.getTime();
if (now.after(calendar1.getTime()) && now.before(calendar2.getTime())) {
return true;
}
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}
You can get your system current time by using this function.
private static String getCurrentTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss",
Locale.getDefault());
return sdf.format(new Date()).toUpperCase();
}
Simpler solution would be to take time bounds in milliseconds. Then take your desired time in milliseconds and do the check lowerBound < desiredTime < upperBound.

Getting day from Datein Java

I am trying to get the day of the week for a particular date, but the day that I am getting is not the right one. This is the part of my code
SimpleDateFormat dayNameFormat = new SimpleDateFormat("EEEE");
date = "some date";
daysName = dayNameFormat.format(sdf.parse(date);
Thanksin advance
Prints FRIDAY...
String dateString = "2016-12-02";
LocalDate localDate = LocalDate.parse(dateString);
DayOfWeek dow = localDate.getDayOfWeek();
System.out.println(dow);

how to get month name using current date as input in function

How do I create a function which take current date and return month name?
I have only date its not current date it can be any date like 2013/4/12 or 23/8/8.
Like String monthName("2013/9/11");
when call this function return the month name.
This should be fine.
It depends on the format of date.
If you try with February 1, 2011
it would work, just change this string "MMMM d, yyyy" according to your needs.
Check this for all format patterns.
And also, months are 0 based, so if you want January to be 1, just return month + 1
private static int getMonth(String date) throws ParseException{
Date d = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(date);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
int month = cal.get(Calendar.MONTH);
return month + 1;
}
If you want month name try this
private static String getMonth(String date) throws ParseException{
Date d = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(date);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
String monthName = new SimpleDateFormat("MMMM").format(cal.getTime());
return monthName;
}
As I said, check web page I posted for all format patterns. If you want only 3 characters of month, use "MMM" instead of "MMMM"
java.time
I am contributing the modern answer.
System.out.println(LocalDate.of(2013, Month.SEPTEMBER, 11) // Define the date
.getMonth() // Get the month
.getDisplayName( // Get the month name
TextStyle.FULL_STANDALONE, // No abbreviation
Locale.ENGLISH)); // In which language?
Output is:
September
Use LocalDate from java.time, the modern Java date and time API, for a date.
Use LocalDate.getMonth() and Month.getDisplayName() to get the month name.
Avoid Date, Calendar and SimpleDateFormat used in the old answers from 2013. Those classes are poorly designed, troublesome and long outdated. The modern API is so much nicer to work with. Also avoid switch/case for this purpose since the month names are already built in, and using the library methods gives you clearer, terser and less error-prone code.
Use LocalDate
LocalDate today = LocalDate.now(ZoneId.systemDefault());
LocalDate aDate = LocalDate.of(2013, Month.SEPTEMBER, 11); // 2013/9/11
LocalDate anotherDate = LocalDate.of(2023, 8, 8); // 23/8/8
If you are getting the date as string input, parse the string using a DateTimeFormatter:
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("u/M/d");
String stringInput = "2013/4/12";
LocalDate date = LocalDate.parse(stringInput, dateFormatter);
System.out.println(date);
2013-04-12
Use LocalDate.getMonth() and Month.getDisplayName()
To get the month name you first need to decide in which language you want the month name. I am taking English as an example and still using date from the previous snippet:
String monthName = date.getMonth()
.getDisplayName(TextStyle.FULL_STANDALONE, Locale.ENGLISH);
System.out.println(monthName);
April
Java knows the month names in a wealth of languages. If you want the month name in the user’s language, pass Locale.getDefault() as the second argument to getDisplayName().
Link
Oracle tutorial: Date Time explaining how to use java.time.
Use this code -
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
int month = calendar.get(Calendar.MONTH);
So now you have month number, you can use switch case to get name for that month.
If your date is in string format use this-
Date date = new SimpleDateFormat("yyyy-MM-dd").format(d)
Simple solution to get current month by name:
SimpleDateFormat formatterMonth = new SimpleDateFormat("MMMM");
String currentMonth = formatterMonth.format(new Date(System.currentTimeMillis()));
Function to get any month by name using format 2013/9/11: (not tested)
private String monthName(String dateToCheck){
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
date = formatter.parse(dateToCheck);
SimpleDateFormat formatterMonth = new SimpleDateFormat("MMMM");
return formatterMonth.format(new Date(date.getTime()));
}
I am using a function like this:
public String getDate(String startDate) throws ParseException {
#SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = null;
try {
d = sdf.parse(startDate);
sdf.applyPattern("MMMM dd, YYYY"); //this gives output as=> "Month date, year"
} catch (Exception e) {
Log.d("Exception", e.getLocalizedMessage());
}
return sdf.format(d);
}
You can obtain the "number" of the month as described in the other answer and then you could simply use a switch to obtain a name.
Example:
switch(month) {
case 0:
your name is January
break;
...
}
P.S. I think months are zero-based but I'm not 100% sure...

How can I get Month Name from Calendar?

Is there a oneliner to get the name of the month when we know:
int monthNumber = calendar.get(Calendar.MONTH)
Or what is the easiest way?
You can achieve it using SimpleDateFormat, which is meant to format date and times:
Calendar cal = Calendar.getInstance();
System.out.println(new SimpleDateFormat("MMM").format(cal.getTime()));
String getMonthForInt(int num) {
String month = "wrong";
DateFormatSymbols dfs = new DateFormatSymbols();
String[] months = dfs.getMonths();
if (num >= 0 && num <= 11) {
month = months[num];
}
return month;
}
As simple as this
mCalendar = Calendar.getInstance();
String month = mCalendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
This is the solution I came up with for a class project:
public static String theMonth(int month){
String[] monthNames = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
return monthNames[month];
}
The number you pass in comes from a Calendar.MONTH call.
If you have multi-language interface, you can use getDisplayName to display the name of month with control of displaying language.
Here is an example of displaying the month name in English, French, Arabic and Arabic in specific country like "Syria":
Calendar c = Calendar.getInstance();
System.out.println(c.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH ) );
System.out.println(c.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.FRANCE ) );
System.out.println(c.getDisplayName(Calendar.MONTH, Calendar.LONG, new Locale("ar") ) );
System.out.println(c.getDisplayName(Calendar.MONTH, Calendar.LONG, new Locale("ar", "SY") ) );
System.out.println(c.getTime().toString());
The result is:
January
janvier
يناير
كانون الثاني
Sat Jan 17 19:31:30 EET 2015
SimpleDateFormat dateFormat = new SimpleDateFormat( "LLLL", Locale.getDefault() );
dateFormat.format( date );
For some languages (e.g. Russian) this is the only correct way to get the stand-alone month names.
This is what you get, if you use getDisplayName from the Calendar or DateFormatSymbols for January:
января (which is correct for a complete date string: "10 января, 2014")
but in case of a stand-alone month name you would expect:
январь
Joda-Time
How about using Joda-Time. It's a far better date-time API to work with (And January means january here. It's not like Calendar, which uses 0-based index for months).
You can use AbstractDateTime#toString( pattern ) method to format the date in specified format:
DateTime date = DateTime.now();
String month = date.toString("MMM");
Month Name From Number
If you want month name for a particular month number, you can do it like this:
int month = 3;
String monthName = DateTime.now().withMonthOfYear(month).toString("MMM");
Localize
The above approach uses your JVM’s current default Locale for the language of the month name. You want to specify a Locale object instead.
String month = date.toString( "MMM", Locale.CANADA_FRENCH );
Month::getDisplayName
Since Java 8, use the Month enum. The getDisplayName method automatically localizes the name of the month.
Pass:
A TextStyle to determine how long or how abbreviated.
A Locale to specify the human language used in translation, and the cultural norms used for abbreviation, punctuation, etc.
Example:
public static String getMonthStandaloneName(Month month) {
return month.getDisplayName(
TextStyle.FULL_STANDALONE,
Locale.getDefault()
);
}
It might be an old question, but as a one liner to get the name of the month when we know the indices, I used
String month = new DateFormatSymbols().getMonths()[monthNumber - 1];
or for short names
String month = new DateFormatSymbols().getShortMonths()[monthNumber - 1];
Please be aware that your monthNumber starts counting from 1 while any of the methods above returns an array so you need to start counting from 0.
This code has language support.
I had used them in Android App.
String[] mons = new DateFormatSymbols().getShortMonths();//Jan,Feb,Mar,...
String[] months = new DateFormatSymbols().getMonths();//January,Februaty,March,...
I found this much easier(https://docs.oracle.com/javase/tutorial/datetime/iso/enum.html)
private void getCalendarMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Month month = Month.of(calendar.get(Calendar.MONTH));
Locale locale = Locale.getDefault();
System.out.println(month.getDisplayName(TextStyle.FULL, locale));
System.out.println(month.getDisplayName(TextStyle.NARROW, locale));
System.out.println(month.getDisplayName(TextStyle.SHORT, locale));
}
This works for me:
String getMonthName(int monthNumber) {
String[] months = new DateFormatSymbols().getMonths();
int n = monthNumber-1;
return (n >= 0 && n <= 11) ? months[n] : "wrong number";
}
To returns "September" with one line:
String month = getMonthName(9);
Calender cal = Calendar.getInstance(Locale.ENGLISH)
String[] mons = new DateFormatSymbols(Locale.ENGLISH).getShortMonths();
int m = cal.get(Calendar.MONTH);
String mName = mons[m];
Easiest Way
import java.text.DateFormatSymbols;
int month = 3; // March
System.out.println(new DateFormatSymbols().getMonths()[month-1]);
You can get it one line like this:
String monthName = new DataFormatSymbols.getMonths()[cal.get(Calendar.MONTH)]
One way:
We have Month API in Java (java.time.Month). We can get by using Month.of(month);
Here, the Month are indexed as numbers so either you can provide by Month.JANUARY or provide an index in the above API such as 1, 2, 3, 4.
Second way:
ZonedDateTime.now().getMonth();
This is available in java.time.ZonedDateTime.
It returns English name of the month.
04 returns APRIL and so on.
String englishMonth (int month){
return Month.of(month);
}
import java.text.SimpleDateFormat;
import java.util.Calendar;
Calendar cal = Calendar.getInstance();
String currentdate=new SimpleDateFormat("dd-MMM").format(cal.getTime());
I created a Kotlin extension based on responses in this topic and using the DateFormatSymbols answers you get a localized response.
fun Date.toCalendar(): Calendar {
val calendar = Calendar.getInstance()
calendar.time = this
return calendar
}
fun Date.getMonthName(): String {
val month = toCalendar()[Calendar.MONTH]
val dfs = DateFormatSymbols()
val months = dfs.months
return months[month]
}
DateFormat date = new SimpleDateFormat("dd/MMM/yyyy");
Date date1 = new Date();
System.out.println(date.format(date1));
For full name of month:
val calendar = Calendar.getInstance()
calendar.timeInMillis = date
return calendar.getDisplayName(Calendar.MONTH, Calendar.Long, Locale.ENGLISH)!!.toString()
And for short name of month:
val calendar = Calendar.getInstance()
calendar.timeInMillis = date
return calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.ENGLISH)!!.toString()
from the SimpleDateFormat java doc:
* <td><code>"yyyyy.MMMMM.dd GGG hh:mm aaa"</code>
* <td><code>02001.July.04 AD 12:08 PM</code>
* <td><code>"EEE, d MMM yyyy HH:mm:ss Z"</code>
* <td><code>Wed, 4 Jul 2001 12:08:56 -0700</code>

How to get full date in android?

I know about to get the date in android with the help of the calender instance.
Calendar c = Calendar.getInstance();
System.out.println("====================Date is:"+ c.get(Calendar.DATE));
But with that i got only the number of the Date. . .
In My Application i have to do Some Calculation based on the Date Formate. Thus if the months get changed then that calculation will be getting wrong.
So for that reason i want the full date that gives the Month, Year and the date of the current date.
And what should be done if i want to do Some Calculation based on that date ?
As like: if the date is less then two weeks then the message should be printed. . .
Please Guide me in this.
Thanks.
Look at here,
Date cal=Calendar.getInstance().getTime();
String date = SimpleDateFormat.getDateInstance().format(cal);
for full date format look SimpleDateFormat
and IF you want to do calculation on date instance I think you should use, Calendar.getTimeInMillis() field on these milliseconds make calculation.
EDIT: these are the formats by SImpleDateFormat class.
String[] formats = new String[] {
"yyyy-MM-dd",
"yyyy-MM-dd HH:mm",
"yyyy-MM-dd HH:mmZ",
"yyyy-MM-dd HH:mm:ss.SSSZ",
"yyyy-MM-dd'T'HH:mm:ss.SSSZ",
};
for (String format : formats) {
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
}
EDIT: two date difference (Edited on Date:09/21/2011)
String startTime = "2011-09-19 15:00:23"; // this is your date to compare with current date
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = dateFormat.parse(startTime);
// here I make the changes.... now Date d use a calendar's date
Date d = Calendar.getInstance().getTime(); // here you can use calendar beco'z date is now deprecated ..
String systemTime =(String) DateFormat.format("yyyy-MM-dd HH:mm:ss", d.getTime());
SimpleDateFormat df1;
long diff = (d.getTime() - date1.getTime()) / (1000);
int Totalmin =(int) diff / 60;
int hours= Totalmin/60;
int day= hours/24;
int min = Totalmin % 60;
int second =(int) diff % 60;
if(day < 14)
{
// your stuff here ...
Log.e("The day is within two weeks");
}
else
{
Log.e("The day is more then two weeks");
}
Thanks.
Use SimpleDateFormat class,
String date = SimpleDateFormat.getDateInstance().format(new Date());
you can use
//try different flags for the last parameter
DateUtils.formatDateTime(context,System.currentTimeMillis(),DateUtils.FORMAT_SHOW_DATE);
for all options check http://developer.android.com/reference/android/text/format/DateUtils.html
try this,
int month = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);
int day = c.get(Calendar.DAY_OF_MONTH);
System.out.println("Current date : "
+ day + "/" + (month + 1) + "/" + year);
}
I'm using following methods to get date and time. You can change the locale here to arabic or wot ever u wish to get date in specific language.
public static String getDate(){
String strDate;
Locale locale = Locale.US;
Date date = new Date();
strDate = DateFormat.getDateInstance(DateFormat.DEFAULT, locale).format(date);
return strDate;
}
public static String getTime(){
String strTime;
Locale locale = Locale.US;
Date date = new Date();
strTime = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale).format(date);
return strTime;
}
you can get the value and save it on String as below
String Date= getDate();
String Time = getTime();

Categories