-2

I need to convert a long value to any date. Next I want the month written out, converted as String. I wanna display the Month on a TextView.

I've tried:

Date dt = new Date();
Calendar cal= Calendar.getInstance();
cal.setTime(dt);  //get current time

long dateAsLong = calendar.getTimeInMillis(); // get currentTime as long

CalendarView.setDate(dateAsLong); // give the view CalendarView the current date 


TextView tv= (TextView) findViewById(R.id.TextViewDate);
tv.setText(String.valueOf(dateAsLong)); // <-- but this is wrong
Marcel Hofgesang
  • 951
  • 1
  • 15
  • 36

4 Answers4

2

tl;dr

convert a long value to … date … month written out

Instant.ofEpochMilli( myMillis )
       .atZone( ZoneId.of( "Pacfic/Auckland" ) ) 
       .getMonth()
       .getDisplayName( TextStyle.FULL , Locale.ITALY )  // Or Locale.US, Locale.UK, etc.

ottobre

Avoid legacy date-time classes

The other Answers use the troublesome old date-time classes that are now legacy, supplanted by the java.time classes.

java.time

Assuming your long value represents a count of milliseconds since the first moment of 1970 in UTC, use Instant.

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Instant instant = Instant.ofEpochMilli( myMillis ) ;

You want a month written out. Determining a month means determining a date. Determining a date requires a time zone. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "Africa/Tunis" ); 
ZonedDateTime zdt = instant.atZone( z ) ;  // Same moment in history, but adjusted into the wall-clock time of a particular region of people.

Now interrogate for the month. The java.time classes include a Month enum, to represent a month January-December.

Month m = zdt.getMonth() ;

The Month enum includes handy methods such as generating a String in an automatically localized format.

String output = m.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH )  ;  // Or Locale.US, Locale.ITALY, etc.

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.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

Use following code:

    long longDate=12334;
    Date date=new Date(longDate);
    String month=new SimpleDateFormat("MMMM").format(date); //you can use month for display

Hope it helps.

Darshan Lila
  • 5,772
  • 2
  • 24
  • 34
1
    Date date = new Date();
    System.out.println(date.getTime());//Timestamp 
    long timestamp = date.getTime();
    date = new Date(timestamp);
    String month = new SimpleDateFormat("MMMM").format(date);//Get month string
    System.out.println(month);
Devan M S
  • 692
  • 9
  • 23
1

First get the date from Long

long val = 1346524199000l;
Date date=new Date(val);
SimpleDateFormat df2 = new SimpleDateFormat("MMM");
String month= df2.format(date);

Once you are having Month in form of string, convert the same into android textView by using the below code

textview.setText(month);
prashant thakre
  • 5,061
  • 3
  • 26
  • 39