I'm trying to parse a String that represents a Date "2017-05-22" into a Date() using SimpleDateFormat(). I also need to have the date conversion in Spanish, so I'm creating a Locale with the spanish configuration. The problem is that for some reason I still get the output in English. My phones Language & Input configuration by default is English, but I've tried to change it to Spanish as well. Here is the full code:
val localeSpanish = Locale("es", "ES")
val dateFormat = SimpleDateFormat("yyyy-MM-dd", localeSpanish)
val dateInSpanish = dateFormat.parse("2017-01-29")
And I get this in the ouput:
Wed Jan 11 00:00:00 GMT-04:00 2017
Can someone tell me what I'm doing wrong?
As stated by others, when you are outputting dateInSpanish, you are exposing a Date instance, which is calling its toString method and its implementation constructs that string based on a static array containing the words in English
//From java.util.Date
private final static String wtb[] = {
"am", "pm",
"monday", "tuesday", "wednesday", "thursday", "friday",
"saturday", "sunday",
"january", "february", "march", "april", "may", "june",
"july", "august", "september", "october", "november", "december",
"gmt", "ut", "utc", "est", "edt", "cst", "cdt",
"mst", "mdt", "pst", "pdt"
};
When you are declaring the dateFormatter, it is meant to convert a date/string as the following example:
val dateFormatter = SimpleDateFormat("yyyy-MM-dd", localeSpanish)
println(dateFormatter.parse("2017-01-29")) // prints: Wed Jan 29 00:00:00 GMT-02:00 2017
println(dateFormatter.format(Date()) // prints: 2018-07-27 (as today :p)
I think you should use a different mask in order to obtain the formatted string.
But if you are forced to read the date in that format, you would have to declare two formatters:
val readerFormatter = SimpleDateFormat("yyyy-MM-dd", localeSpanish)
val writerFormatter = SimpleDateFormat("d 'de' MMMM 'del' yyyy", localeSpanish)
val readDate: Date = readerFormatter.parse("2017-01-29")
val dateInSpanish: String = writerFormatter.format(readDate)
println(dateInSpanish) // prints: 29 de enero del 2017 (as today :p)
Sorry I cannot write Kotlin (yet). Can you translate form Java?
Locale localeSpanish = new Locale("es", "ES");
DateTimeFormatter dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG)
.withLocale(localeSpanish);
LocalDate date = LocalDate.parse("2017-01-29");
String formattedDate = date.format(dateFormatter);
System.out.println("Fecha en español: " + formattedDate);
This prints:
Fecha en español: 29 de enero de 2017
Messages:
The date-time classes you were using, SimpleDateFormat and Date, are long outdated and poorly designed. SimpleDateFormat in particular has a reputation for being troublesome. And despite the name a Date represents a point in time, not a date. Instead use java.time, the modern Java date and time API.
For a date without time of day use the LocalDate class.
The format you are parsing, yyyy-MM-dd, is ISO 8601. The modern classes parse ISO 8601 as their default, that is, without any explicit formatter.
No matter if you use Date or LocalDate, they haven’t got neither a format nor a locale in them. They just hold the data in much the same way as an int holds a number without any format or locale. No matter if you have an int, a Date or a LocalDate, if you want a specific format, you can have that format only in a String.
To format a date for an audience in a locale, use a built-in date format. DateTimeFormatter.ofLocalizedDate gives you one, but you need to convert it to the desired locale.
Question: Can I use java.time on Android?
Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.
In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
Wikipedia article: ISO 8601
Simple, SimpleDateFormat.parse returns a Date object which is format agnostic. What you want to do is print the following:
val dateString = dateFormat.format(dateInSpanish)
Try using this for date representation and for local spanish time use your localSpanish val
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy MMM dd - HH:mm:ss",*your code here*);
String formattedDate = df.format(c.getTime());
The line:
val dateInSpanish = dateFormat.parse("2017-01-29")
does not format the date, it just extracts the date out of the string
The line:
val localeSpanish = Locale("es", "ES")
does not change your phone's configuration
so you have a date: dateInSpanish
and you can format it as you like and then print it
Related
I have a date in this format - 01 May 2020, and I want to parse it in the following format - 2020-05-01T00:00:00. I am using the following code -
static String convertDateStringFormat(String dateString, String originalDateFormat, String outputDateFormat){
DateFormat inputFormat = new SimpleDateFormat(originalDateFormat, Locale.ENGLISH);
Date input = null;
try {
input = inputFormat.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
DateFormat outputFormat = new SimpleDateFormat(outputDateFormat, Locale.ENGLISH);
return outputFormat.format(input);
}
and I am passing the arguments as - convertDateStringFormat("01 May 2020", "dd MMM yyyy", "yyyy-MM-dd'T'HH:mm:ss"). I dont know what I am doing wrong but it gives me this exception - java.lang.IllegalArgumentException: Parse error: 2020-05-01T00:00:00, so it looks like it is converting to the right format and then throwing an exception?
Build a format using DateTimeFormatterBuilder with hour defaulted to 0 and then parse the date string into LocalDateTime.
Demo:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
public class Main {
public static void main(String[] args) {
DateTimeFormatter format = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ofPattern("dd MMM yyyy"))
.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
.toFormatter();
String strDate = "01 May 2020";
LocalDateTime date = LocalDateTime.parse(strDate, format);
DateTimeFormatter targetFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
String strDateInTargetFormat = date.format(targetFormat);
System.out.println(strDateInTargetFormat);
}
}
Output:
2020-05-01T00:00:00
tl;dr
LocalDate
.parse(
"01 May 2020" ,
DateTimeFormatter
.ofPattern( "dd MMMM uuuu" )
.withLocale( Locale.US )
)
.atStartOfDay(
ZoneId.of( "Africa/Tunis" )
)
.format(
DateTimeFormatter.ISO_LOCAL_DATE_TIME
)
See this code run live at IdeOne.com.
2020-05-01T00:00:00
Details
You said:
I have a date in this format - 01 May 2020
That means you have text, a String, not a date. Date-time objects do not have a “format”.
Parse your text as a LocalDate.
String input = "01 May 2020" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd MMMM uuuu" ).withLocale( Locale.US ) ;
LocalDate ld = LocalDate.parse( input , f ) ;
You said:
want to parse it in the following format - 2020-05-01T00:00:00.
No, that is not parsing. That is generating text.
But first we need to determine a time-of-day. I presume you want the first moment of the day.
Some dates in some time zones do not start at 00:00. So always let java.time determine the first moment of the day. We capture this moment as a ZonedDateTime object.
ZoneId z = ZoneId.of( "Asia/Amman" ) ;
ZonedDateTime zdt = ld.atStartOfDay( z ) ;
You ask for text to be generated in a format with no indication of time zone or offset-from-UTC. I recommend against this, as the reading of such text is ambiguous. But if you insist, java.time includes a predefined format for the kind of text you desire. This format is amongst those defined in ISO 8601.
String output = zdt.format( DateTimeFormatter.ISO_LOCAL_DATE_TIME ) ;
See this code run live at IdeOne.com.
output: 2020-05-01T00:00:00
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
Java 9 adds some minor features and fixes.
Java SE 6 and Java SE 7
Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
Later versions of Android bundle implementations of the java.time classes.
For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
Use like that to convert one format to another:
DateFormat originalFormat = new SimpleDateFormat("dd MMM yyyy", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = originalFormat.parse("01 May 2020");
String formattedDate = targetFormat.format(date);
Having input date string could be possible as
"2020-01-25T21:59:27Z"
or
"Sat Jan 25 20:06:07 +0000 2020"
or a Long
and expect one to display is like
Jan 25, 2020
how to get the desired formatted date string?
update:
#Ole V.V provided a very good suggestion, it's just cannt apply it with android lib case.
but I guess there is no single format for all these three cases, so have to try out one by one. such as for the ISO8601 one to do something like:
return try {
val dateStr = "Sat Jan 25 20:06:07 +0000 2020". //ISO8601
val format = SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.getDefault())
val dsipFormat = SimpleDateFormat("MMM dd yyyy", Locale.getDefault()) // for display result
val date = format.parse(dateStr) // parse it to date
dsipFormat.format(date) // returning the display result
} catch (e: Exception) {
Log.e("+++", "+++ error: $e")
""
}
If there is better approach?
java.time and ThreeTenABP
My solution is to build three formatters for the three possible input formats and then for each input try the formatters in turn. For a simple demomstration of the idea:
DateTimeFormatter[] inputFormatters = {
DateTimeFormatter.ISO_INSTANT,
DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss xx yyyy", Locale.ROOT),
new DateTimeFormatterBuilder()
.appendValue(ChronoField.INSTANT_SECONDS)
.appendValue(ChronoField.MILLI_OF_SECOND, 3)
.toFormatter()
};
DateTimeFormatter displayFormatter
= DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
.withLocale(Locale.US);
for (String inputString : new String[] {
"2020-01-25T21:59:27Z",
"Sat Jan 25 20:06:07 +0000 2020",
"1566777888999"
}) {
// try the formatters in turn and see which one works
for (DateTimeFormatter formatter : inputFormatters) {
try {
ZonedDateTime dateTime = formatter.parse(inputString, Instant.FROM)
.atZone(ZoneId.systemDefault());
System.out.format("%-30s was parsed to %s%n",
inputString, dateTime.format(displayFormatter));
break;
} catch (DateTimeParseException ignore) {
// Ignore, try next format
}
}
}
In my time zone (Europe/Copenhagen) output from this snippet is:
2020-01-25T21:59:27Z was parsed to Jan 25, 2020
Sat Jan 25 20:06:07 +0000 2020 was parsed to Jan 25, 2020
1566777888999 was parsed to Aug 26, 2019
Since it is never the same date in all time zones, output will vary with time zone.
I am recommending java.time, the modern Java date and time API. I saw that you tagged the question simpledateformat, but the SimpleDateFormat class is notoriously troublesome and long outdated, so I recommend against using it. And I am exploiting the fact that your first format is standard ISO 8601 and that java.time has a built-in formatter for it, DateTimeFormatter.ISO_INSTANT.
My third input formatter, the one for the long value, regards the last three characters as milliseconds of the second and everything before it as seconds since the epoch. The net result is that it parses milliseconds since the epoch. A DateTimeFormatterBuilder was required to build this formatter.
A no-lib solution
I admit that I hate to write this. I would really have hoped that you could avoid the notoriously troublesome SimpleDateFormat class and its long outdated cronies like Date. Since I understand that yours is a no-lib app, both of Joda-Time and ThreeTenABP seem out of the question. Sorry. In this case since there is no way that SimpleDateFormat can parse a long, my approach is to take a taste of the string to determine the format and choose my way of parsing based on that.
DateFormat inputIso = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
// This format resembles the output from Date.toString
DateFormat inputDatelike
= new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy", Locale.ROOT);
DateFormat displayFormat
= DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US);
displayFormat.setTimeZone(TimeZone.getDefault());
for (String inputString : new String[] {
"2020-01-25T21:59:27Z",
"Sat Jan 25 20:06:07 +0000 2020",
"1566777888999"
}) {
Date parsedDate;
if (Character.isDigit(inputString.charAt(0))) {
if (inputString.contains("-")) {
parsedDate = inputIso.parse(inputString);
} else {
// long number of millis
parsedDate = new Date(Long.parseLong(inputString));
}
} else {
parsedDate = inputDatelike.parse(inputString);
}
System.out.format("%-30s was parsed to %s%n",
inputString, displayFormat.format(parsedDate));
}
Output is exactly the same as before:
2020-01-25T21:59:27Z was parsed to Jan 25, 2020
Sat Jan 25 20:06:07 +0000 2020 was parsed to Jan 25, 2020
1566777888999 was parsed to Aug 26, 2019
Please be aware that here invalid input may cause either a NumberFormatException or a ParseException, so catch both. And only resort to this solution if there is no way that you can avoid it.
The line displayFormat.setTimeZone(TimeZone.getDefault()); is technically superfluous, but it makes explicit that the output depends on the time zone, and maybe more importantly, it tells you where you need to modify the code if you want output in a different time zone.
Question: Doesn’t java.time require Android API level 26?
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in. Only in this case use the method reference Instant::from instead of the constant Instant.FROM.
In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
I am getting date time from rest api as like this "2020-02-13T16:57:13.04 . How can i convert in java for android? So that i can use only date or time separately?
I have tried by this way
String dateStr = rowsArrayList.get(position).getDisplayDate();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
df.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = null;
try {
date = df.parse(dateStr);
df.setTimeZone(TimeZone.getDefault());
String formattedDate = df.format(date);
Log.d("testDate",formattedDate);
} catch (ParseException e) {
e.printStackTrace();
Log.d("testDate",e.toString());
}
But getting the error
java.text.ParseException: Unparseable date: "2020-02-13T16:57:13.04"
Use yyyy-MM-dd'T'HH:mm:ss.SS as format instead of yyyy-MM-dd'T'HH:mm:ss'Z'
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS", Locale.ENGLISH);
java.time
You can also achieve this with java.time, the modern Java date and time API because the old classes (Date, Calendar and SimpleDateFormat) have lots of problems and design issues:
String sourceDateTime = "2020-02-13T16:57:13.04";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SS").withZone(DateTimeZone.UTC);
LocalDateTime localDateTime = LocalDateTime.parse(sourceDateTime, dateTimeFormatter);
LocalDate localDate = localDateTime.toLocalDate();
LocalTime localTime = localDateTime.toLocalTime();
System.out.println(localDate.toString() + " -> " + localTime.toString());
//Output should be: 2020-02-13 -> 16:57:13.040
Question: Can I use java.time on Android?
From Android 8.0 (API level 26) the modern API comes built-in.
For older android version, you can use ThreeTenABP, Android edition of ThreeTen Backport
SimpleDateFormat cannot parse your date-time string correctly.
SimpleDateFormat is notoriously troublesome and long outdated. Neither for this nor for any other purpose should you use it. Instead just use the LocalDateTime class from java.time, the modern Java date and time API. It parses your string wothout any explicit formatter.
You can’t with SimpleDateFormat
Your string has two decimals on the second of minute, .04, signifying 4 hundredths of a second. SimpleDateFormat only supports exactly three decimals on the seconds, not two or four or any other number. So there is no way that it can parse your string correctly.
java.time
It seems that you are assuming that the string you parse is in UTC and you want to convert it to the default time zone of your device. Your string is in ISO 8601 format, the format that the classes of java.time parse as their default, so we don’t need to specify any formatter.
String dateStr = "2020-02-13T16:57:13.04";
LocalDateTime dateTime = LocalDateTime.parse(dateStr);
ZonedDateTime inDefaultTimeZone = dateTime.atOffset(ZoneOffset.UTC)
.atZoneSameInstant(ZoneId.systemDefault());
System.out.println(inDefaultTimeZone);
On my computer in Europe/Copenhagen time zone the output from this snippet is:
2020-02-13T17:57:13.040+01:00[Europe/Copenhagen]
Question: Doesn’t java.time require Android API level 26?
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
Wikipedia article: ISO 8601
Take out the 'Z' on your format string
This should work:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
Or if you want to keep the milliseconds then use SS:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS", Locale.ENGLISH);
String dateStr = "Jul 16, 2013 12:08:59 AM";
SimpleDateFormat df = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss a", Locale.ENGLISH);
df.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = df.parse(dateStr);
df.setTimeZone(TimeZone.getDefault());
String formattedDate = df.format(date);
im trying to convert a string(with unix timestamp) to an date with the format ( dd-MM-yyyy)
and this is working partly. The problem im having now is that my date is in 17-01-1970 (instead of march 16 2015)
im converting it like this:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date d = null;
int dateMulti = Integer.parseInt(Date);
Calendar cal = Calendar.getInstance(Locale.ENGLISH);
cal.setTimeInMillis(dateMulti);
String date = DateFormat.format("dd-MM-yyyy", cal).toString();
Log.d("test",date);
try {
d = dateFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
where Date = 1427101853
and the result = 17-01-1970
what am i doing wrong?
You are using the wrong format string in the first line:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
mm is minutes. Use MM (months) instead.
edit A Unix timestamp is a number of seconds since 01-01-1970 00:00:00 GMT. Java measures time in milliseconds since 01-01-1970 00:00:00 GMT. You need to multiply the Unix timestamp by 1000:
cal.setTimeInMillis(dateMulti * 1000L);
Why you have "dd-mm-yyyy" in SimpleDateFormat and "dd-MM-yyyy" in DateFormat.format? Use this :
String date = DateFormat.format("dd-mm-yyyy", cal).toString();
If you want minutes, if you want months you have to put MM like #Jesper said :)
I should like to contribute the modern answer.
java.time
DateTimeFormatter dateFormatter = DateTimeFormatter
.ofLocalizedDate(FormatStyle.MEDIUM)
.withLocale(Locale.forLanguageTag("da"));
String unixTimeStampString = "1427101853";
int dateMulti = Integer.parseInt(unixTimeStampString);
ZonedDateTime dateTime = Instant.ofEpochSecond(dateMulti)
.atZone(ZoneId.of("Africa/Conakry"));
String formattedDate = dateTime.format(dateFormatter);
System.out.println(formattedDate);
The output from this snippet is:
23-03-2015
The output agrees with an online converter (link at the bottom). It tells me your timestamp equals “03/23/2015 # 9:10am (UTC)” (it also agrees with the date you asked the question). Please substitute your time zone if it didn’t happen to be Africa/Conakry.
The date-time classes that you were using — SimpleDateFormat, Date and Calendar — are long outdated and poorly designed, so I suggest you skip them and use java.time, the modern Java date and time API, instead. A minor one among the many advantages is it accepts seconds since the epoch directly, so you don’t need to convert to milliseconds. While this was no big deal, doing your own time conversions is a bad habit, you get clearer, more convincing and less error-prone code from leaving the conversions to the appropriate library methods.
Question: Can I use java.time on Android?
Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.
In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.
I wrote and ran the above snippet using the backport to make sure it would be compatible with ThreeTenABP.
Links
Timestamp Converter
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
I was also facing the same issue when I was using SimpleDateFormat Here is a method I have made, which is working fine for me.
private String getmDate(long time1) {
java.util.Date time = new java.util.Date((long) time1 * 1000);
String date = DateFormat.format("dd-MMM-yyyy' at 'HH:mm a", time).toString();
return date + "";
}
you can change the date format as you desire.
I'm trying to return the name of the month as a String, for instance "May", "September", "November".
I tried:
int month = c.get(Calendar.MONTH);
However, this returns integers (5, 9, 11, respectively). How can I get the month name?
Use this :
Calendar cal=Calendar.getInstance();
SimpleDateFormat month_date = new SimpleDateFormat("MMMM");
String month_name = month_date.format(cal.getTime());
Month name will contain the full month name,,if you want short month name use
this
SimpleDateFormat month_date = new SimpleDateFormat("MMM");
String month_name = month_date.format(cal.getTime());
For getting month in string variable use the code below
For example the month of September:
M -> 9
MM -> 09
MMM -> Sep
MMMM -> September
String monthname=(String)android.text.format.DateFormat.format("MMMM", new Date())
Use getDisplayName.
For earlier API's use String.format(Locale.US,"%tB",c);
"MMMM" is definitely NOT the right solution (even if it works for many languages), use "LLLL" pattern with SimpleDateFormat
The support for 'L' as ICU-compatible extension for stand-alone month names was added to Android platform on Jun. 2010.
Even if in English there is no difference between the encoding by 'MMMM' and 'LLLL', your should think about other languages, too.
E.g. this is what you get, if you use Calendar.getDisplayName or the "MMMM" pattern for January with the Russian Locale:
января (which is correct for a complete date string: "10 января, 2014")
but in case of a stand-alone month name you would expect:
январь
The right solution is:
SimpleDateFormat dateFormat = new SimpleDateFormat( "LLLL", Locale.getDefault() );
dateFormat.format( date );
If you are interested in where all the translations come from - here is the reference to gregorian calendar translations (other calendars linked on top of the page).
As simple as this
mCalendar = Calendar.getInstance();
String month = mCalendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
Calendar.LONG is to get the full name of the month and Calendar.SHORT gives the name in short.
For eg: Calendar.LONG will return January
Calendar.SHORT will return Jan
I keep this answer which is useful for other cases, but #trutheality answer seems to be the most simple and direct way.
You can use DateFormatSymbols
DateFormatSymbols(Locale.FRENCH).getMonths()[month]; // FRENCH as an example
The only one way on Android to get properly formatted stanalone month name for such languages as ukrainian, russian, czech
private String getMonthName(Calendar calendar, boolean short) {
int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_MONTH_DAY | DateUtils.FORMAT_NO_YEAR;
if (short) {
flags |= DateUtils.FORMAT_ABBREV_MONTH;
}
return DateUtils.formatDateTime(getContext(), calendar.getTimeInMillis(), flags);
}
Tested on API 15-25
Output for May is Май but not Мая
Russian.
Month
.MAY
.getDisplayName(
TextStyle.FULL_STANDALONE ,
new Locale( "ru" , "RU" )
)
май
English in the United States.
Month
.MAY
.getDisplayName(
TextStyle.FULL_STANDALONE ,
Locale.US
)
May
See this code run live at IdeOne.com.
ThreeTenABP and java.time
Here’s the modern answer. When this question was asked in 2011, Calendar and GregorianCalendar were commonly used for dates and times even though they were always poorly designed. That’s 8 years ago now, and those classes are long outdated. Assuming you are not yet on API level 26, my suggestion is to use the ThreeTenABP library, which contains an Android adapted backport of java.time, the modern Java date and time API. java.time is so much nicer to work with.
Depending on your exact needs and situation there are two options:
Use Month and its getDisplayName method.
Use a DateTimeFormatter.
Use Month
Locale desiredLanguage = Locale.ENGLISH;
Month m = Month.MAY;
String monthName = m.getDisplayName(TextStyle.FULL, desiredLanguage);
System.out.println(monthName);
Output from this snippet is:
May
In a few languages it will make a difference whether you use TextStyle.FULL or TextStyle.FULL_STANDALONE. You will have to see, maybe check with your users, which of the two fits into your context.
Use a DateTimeFormatter
If you’ve got a date with or without time of day, I find a DateTimeFormatter more practical. For example:
DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("MMMM", desiredLanguage);
ZonedDateTime dateTime = ZonedDateTime.of(2019, 5, 31, 23, 49, 51, 0, ZoneId.of("America/Araguaina"));
String monthName = dateTime.format(monthFormatter);
I am showing the use of a ZonedDateTime, the closest replacement for the old Calendar class. The above code will work for a LocalDate, a LocalDateTime, MonthDay, OffsetDateTime and a YearMonth too.
What if you got a Calendar from a legacy API not yet upgraded to java.time? Convert to a ZonedDateTime and proceed as above:
Calendar c = getCalendarFromLegacyApi();
ZonedDateTime dateTime = DateTimeUtils.toZonedDateTime(c);
The rest is the same as before.
Question: Doesn’t java.time require Android API level 26?
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
I would recommend to use Calendar object and Locale, because month names are different for different languages:
// index can be in range 0 - 11
private String getMonthName(final int index, final Locale locale, final boolean shortName)
{
String format = "%tB";
if (shortName)
format = "%tb";
Calendar calendar = Calendar.getInstance(locale);
calendar.set(Calendar.MONTH, index);
calendar.set(Calendar.DAY_OF_MONTH, 1);
return String.format(locale, format, calendar);
}
Example for full month name:
System.out.println(getMonthName(0, Locale.US, false));
Result: January
Example for short month name:
System.out.println(getMonthName(0, Locale.US, true));
Result: Jan
A sample way to get the date and time in
this format "2018 Nov 01 16:18:22" use this
DateFormat dateFormat = new SimpleDateFormat("yyyy MMM dd HH:mm:ss");
Date date = new Date();
dateFormat.format(date);
Getting a standalone month name is surprisingly difficult to perform "right" in Java. (At least as of this writing. I'm currently using Java 8).
The problem is that in some languages, including Russian and Czech, the standalone version of the month name is different from the "formatting" version. Also, it appears that no single Java API will just give you the "best" string. The majority of answers posted here so far only offer the formatting version. Pasted below is a working solution for getting the standalone version of a single month name, or getting an array with all of them.
I hope this saves someone else some time!
/**
* getStandaloneMonthName, This returns a standalone month name for the specified month, in the
* specified locale. In some languages, including Russian and Czech, the standalone version of
* the month name is different from the version of the month name you would use as part of a
* full date. (Different from the formatting version).
*
* This tries to get the standalone version first. If no mapping is found for a standalone
* version (Presumably because the supplied language has no standalone version), then this will
* return the formatting version of the month name.
*/
private static String getStandaloneMonthName(Month month, Locale locale, boolean capitalize) {
// Attempt to get the standalone version of the month name.
String monthName = month.getDisplayName(TextStyle.FULL_STANDALONE, locale);
String monthNumber = "" + month.getValue();
// If no mapping was found, then get the formatting version of the month name.
if (monthName.equals(monthNumber)) {
DateFormatSymbols dateSymbols = DateFormatSymbols.getInstance(locale);
monthName = dateSymbols.getMonths()[month.getValue()];
}
// If needed, capitalize the month name.
if ((capitalize) && (monthName != null) && (monthName.length() > 0)) {
monthName = monthName.substring(0, 1).toUpperCase(locale) + monthName.substring(1);
}
return monthName;
}
/**
* getStandaloneMonthNames, This returns an array with the standalone version of the full month
* names.
*/
private static String[] getStandaloneMonthNames(Locale locale, boolean capitalize) {
Month[] monthEnums = Month.values();
ArrayList<String> monthNamesArrayList = new ArrayList<>();
for (Month monthEnum : monthEnums) {
monthNamesArrayList.add(getStandaloneMonthName(monthEnum, locale, capitalize));
}
// Convert the arraylist to a string array, and return the array.
String[] monthNames = monthNamesArrayList.toArray(new String[]{});
return monthNames;
}
It will provide current date and month:
fun getDateTime(): String?
{
val dateFormat: DateFormat = SimpleDateFormat("dd MMMM")
val date = Date()
return dateFormat.format(date)
}