0

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)

GuiceU
  • 1,030
  • 6
  • 19
  • 35
  • 1
    You could use `jodatime`, but check for this http://stackoverflow.com/questions/5059663/android-java-joda-date-is-slow – Hernán Eche Dec 21 '12 at 15:05
  • In Java you can use `GregorianCalendar`, or `JodaTime`. I do not know if it has been ported to android, though. – SJuan76 Dec 21 '12 at 15:06

2 Answers2

7

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);
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Yeaaah, it works! But since I get my date from an Cursor, and have to first format it to a String with df.format(); Then it returns a String like this: "2012-12-21", and with your method it needs to be like this: "2012-Dec-21". But maybe I can change that in someway? – GuiceU Dec 21 '12 at 15:26
  • Naaah, sorry! My bad! Did'nt notice the 'yyyy-MMM-dd' in the 'DateFormat dateFormatter = new SimpleDateFormat("yyyy-MMM-dd");' So thanks dude! – GuiceU Dec 21 '12 at 16:06
  • Merry Christmas. Accept the answer if it helped - good for you, good for me. – duffymo Dec 21 '12 at 16:39
1

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
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154