Matan Retzer
Matan Retzer

Reputation: 65

How can I extract the week number from java.util.Date in scala

I already tried to use: getWeek getCalendarDate and I don't want to use Calendar package (because I am using a specific software that don't allow me to use it) Thanks in advance,

Upvotes: 0

Views: 5642

Answers (4)

Matan Retzer
Matan Retzer

Reputation: 65

import java.util.Calendar
import java.util.GregorianCalendar 

val calendar = new GregorianCalendar()

calendar.setTime(sail_dt)   
calendar.get(Calendar.WEEK_OF_YEAR)

Finally I managed to find a SCALA code to cope with my problem. Thank you all.

Upvotes: 2

Basil Bourque
Basil Bourque

Reputation: 338326

tl;dr

org.threeten.extra.YearWeek.from(
    myJavaUtilDate.toInstant()
                  .atZone( ZoneId.of( "Pacific/Auckland" )  )
).getWeek()

49

YearWeek

The Answer by Ole V.V. is correct and wise.

(Java syntax here; do not know Scala)

If doing much work with weeks, and your definition of week agrees with the ISO 8601 definition of week where week # 1 contains the first Thursday and where Monday is the first day-of-week, then consider adding the ThreeTen-Extra library to your project. That library includes many classes extending the functionality of the java.time classes. In particular is a YearWeek class.

First convert the awful troubled java.util.Date object to java.time.

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 = myJavaUtilDate.toInstant();

Assign the time zone through which you want to perceive a date. A time zone is crucial in determining a date. 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( "America/Montreal" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

Generate the YearWeek object to represent that moment’s year-week in the ISO 8601 week date system, the combination of a week-based-year and week-of-week-based-year.

YearWeek yw = YearWeek.from( zdt ) ;

If you want to represent that value with a standard ISO 8601 format, simply call YearWeek::toString.

String output = yw.toString() ;

2017-W49

If you want to represent that value with the week number as an integer, call YearWeek::getWeek.

int weekNumber = yw.getWeek() ;

49

Avoid legacy date-time classes

By the way, you should indeed be avoiding the Calendar class, as well as Date, SimpleDateFormat, and the java.sql classes. All those troublesome old date-time classes are now legacy, entirely supplanted by the java.time classes.


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.

Upvotes: 0

Anonymous
Anonymous

Reputation: 86223

I am sorry that I cannot write Scala code. I will have to trust you to adapt from my Java snippets.

If you can, abandon the long outdated Date class. Use for example ZonedDateTime from java.time, the modern Java date and time API.

    int weekNumber = myZonedDateTime.get(WeekFields.ISO.weekOfYear());

The same will work with an OffsetDateTime, a LocalDateTime and a LocalDate. For a date or date-time today (Dec 6, 2017), you get 49.

I used ISO 8601 weeks (Monday is the first day of week, and week 1 is the first week that has at least 4 days in January). If your weeks are different, please substitute a different WeekFields object, for example WeekFields.SUNDAY_START.

If you cannot avoid getting an old-fashioned Date object, convert it to a modern Instant and do the further operations using the modern API:

    ZonedDateTime converted = oldFashionedDate.toInstant()
            .atZone(ZoneId.of("Pacific/Nauru"));

Please substitute your desired time zone (provided that you didn’t want Pacific/Nauru). Once you’ve got a ZonedDateTime, proceed as above.

Upvotes: 2

sarveshseri
sarveshseri

Reputation: 13985

Here is the API documentation for JavaDoc - java.util.Date

You should be able to see that java.utils.Date has no method for getting week.

People generally use Calender for various requirements. In your case, you can use a DateFormat to format your Date object to a String then parse it to get the integer value.

import java.text.SimpleDateFormat
import java.util.Date

val date = new Date()

// if you want week in year
val formatter1 = new SimpleDateFormat("w")
val week1 = Integer.parseInt(formatter1.format(date))

// if you want week in month
val formatter2 = new SimpleDateFormat("W")
val week2 = Integer.parseInt(formatter2.format(date))

Upvotes: 2

Related Questions