Guest User

Untitled

a guest
Feb 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. Calendar c = Calendar.getInstance();
  2. DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
  3. c.setTime( sdf.parse("31/12/2010"));
  4. out.println( c.get( Calendar.WEEK_OF_YEAR ) );
  5.  
  6. public static void main(String[] args) throws ParseException {
  7.  
  8. DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
  9. Date lastDec2010 = sdf.parse("31/12/2010");
  10.  
  11. Calendar calUs = Calendar.getInstance(Locale.US);
  12. calUs.setTime(lastDec2010);
  13.  
  14. Calendar calDe = Calendar.getInstance(Locale.GERMAN);
  15. calDe.setTime(lastDec2010);
  16.  
  17. System.out.println( "us: " + calUs.get( Calendar.WEEK_OF_YEAR ) );
  18. System.out.println( "de: " + calDe.get( Calendar.WEEK_OF_YEAR ) );
  19. }
  20.  
  21. us: 1
  22. de: 52
  23.  
  24. Calendar c = Calendar.getInstance();
  25. c.setMinimalDaysInFirstWeek(7);//anything more than 1 will work in this year
  26. DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
  27. c.setTime( sdf.parse("31/12/2010"));
  28. System.out.println( c.get( Calendar.WEEK_OF_YEAR ) );
  29.  
  30. 52
  31.  
  32. java.time.LocalDate.parse(
  33. "31/12/2010" ,
  34. DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT ).withLocale( Locale.UK )
  35. )
  36. .get( IsoFields.WEEK_OF_WEEK_BASED_YEAR )
  37.  
  38. org.threeten.extra.YearWeek.from( // Convert from a `LocalDate` object to a `YearWeek` object representing the entire week of that date’s week-based year.
  39. java.time.LocalDate.parse( "31/12/2010" , DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT ).withLocale( Locale.UK )
  40. ).getWeek() // Extract an integer number of that week of week-based-year, either 1-52 or 1-53 depending on the year.
  41.  
  42. ZoneId zoneId = ZoneId.of ( "America/Montreal" );
  43. ZonedDateTime now = ZonedDateTime.now ( zoneId );
  44.  
  45. int week = now.get ( IsoFields.WEEK_OF_WEEK_BASED_YEAR );
  46. int weekYear = now.get ( IsoFields.WEEK_BASED_YEAR );
  47.  
  48. LocalDate ld = LocalDate.parse( "2010-12-31" ) ;
  49. int weekOfWeekBasedYear = ld.get( IsoFields.WEEK_OF_WEEK_BASED_YEAR ) ;
  50. int yearOfWeekBasedYear = ld.get( IsoFields.WEEK_BASED_YEAR ) ;
  51.  
  52. String outputWeek = String.format( "%04d" , yearOfWeekBasedYear ) + "-W" + String.format( "%02d" , weekOfWeekBasedYear ) ;
  53. String outputDate = outputWeek + "-" + ld.getDayOfWeek().getValue() ;
  54.  
  55. YearWeek yw = YearWeek.from( ld ) ; // Determine ISO 8601 week of a `LocalDate`.
  56.  
  57. String output = yw.toString() ;
  58.  
  59. YearWeek yearWeek = YearWeek.parse( "2010-W52" ) ;
  60.  
  61. LocalDate localDate = yw.atDay( DayOfWeek.MONDAY ) ;
Add Comment
Please, Sign In to add comment