Reputation: 1038
Now it's like this, I have a long
variable with date and time on it. So I use DateFormat to get the date and time for itself, and now I want to get the year, month, day, hour and minutes for itself. The date and time is in a String
, so actually I could just cut the string with substring
and all that. But since like for example Sweden has their date like this: "dd/mm/yyyy", and the US has their date like this: "mm/dd/yyyy". So if I just cut the String for the Swedish date, the day variable will become the month.
This is my code:
String start = mCursor.getLong(1);
Format df = DateFormat.getDateFormat(this);
Format tf = DateFormat.getTimeFormat(this);
String date = df.format(start);
String time = tf.format(start);
So my question is, is there someway to get the year, month, day, hour and minute for itself in a own String?
Thanks in advance, GuiceU. (Bad english, I know)
Upvotes: 0
Views: 1155
Reputation: 338785
Looks like both the question and accepted answer ignore time zone, which is probably a problem.
This work is much easier with Joda-Time or the new java.time.* package in Java 8. You said you have long
primitive value, so let's start there assuming it represents milliseconds from Unix epoch (1970). No need to deal with creating or parsing string representations of the date at all. Joda-Time offers many points of access to year, month, etc. numbers and names. And Joda-Time localizes the names to Swedish or English.
The date style code is a pair of characters. The first character is the date style, and the second character is the time style. Specify a character of 'S' for short style, 'M' for medium, 'L' for long, and 'F' for full. A date or time may be omitted by specifying a style character '-'.
long input = DateTime.now().getMillis();
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Stockholm" );
DateTime dateTime = new DateTime( input, timeZone );
int year = dateTime.getYear();
int month = dateTime.getMonthOfYear(); // 1-based counting, 1 = January. Unlike crazy java.util.Date/Calendar.
int dayOfMonth = dateTime.getDayOfMonth();
int hourOfDay = dateTime.getHourOfDay();
int minuteOfHour = dateTime.getMinuteOfHour();
java.util.Locale localeSweden = new Locale( "sv", "SE" ); // ( language code, country code );
String monthName_Swedish = dateTime.monthOfYear().getAsText( localeSweden );
String monthName_UnitedStates = dateTime.monthOfYear().getAsText( java.util.Locale.US );
DateTimeFormatter formatter_Sweden = DateTimeFormat.forStyle( "LM" ).withLocale( localeSweden ).withZone( timeZone );
DateTimeFormatter formatter_UnitedStates_NewYork = DateTimeFormat.forStyle( "LM" ).withLocale( java.util.Locale.US ).withZone( DateTimeZone.forID( "America/New_York" ) );
String text_Swedish = formatter_Sweden.print( dateTime );
String text_UnitedStates_NewYork = formatter_UnitedStates_NewYork.print( dateTime );
DateTime dateTimeUtcGmt = dateTime.withZone( DateTimeZone.UTC );
Dump to console…
System.out.println( "dateTime: " + dateTime );
System.out.println( "monthName_Swedish: " + monthName_Swedish );
System.out.println( "monthName_UnitedStates: " + monthName_UnitedStates );
System.out.println( "text_Swedish: " + text_Swedish );
System.out.println( "text_UnitedStates_NewYork: " + text_UnitedStates_NewYork );
System.out.println( "dateTimeUtcGmt: " + dateTimeUtcGmt );
When run…
dateTime: 2014-02-16T07:12:19.301+01:00
monthName_Swedish: februari
monthName_UnitedStates: February
text_Swedish: den 16 februari 2014 07:12:19
text_UnitedStates_NewYork: February 16, 2014 1:12:19 AM
dateTimeUtcGmt: 2014-02-16T06:12:19.301Z
Upvotes: 1
Reputation: 308813
I wouldn't do it that way. Use java.util.Calendar
:
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MMM-dd");
dateFormatter.setLenient(false);
String dateStr = "2012-Dec-21";
Date date = dateFormatter.parse(dateStr);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int month = calendar.get(Calendar.MONTH);
Upvotes: 7