0

im creating a date from weeknumber, and a day of the week only. this I have successfully done with SimpleDateFormat, but i want to save it as jodatime, i have tried many things, but nothing that really worked.

This is my code so far.

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.WEEK_OF_YEAR, week_of_year);
cal.set(Calendar.DAY_OF_WEEK, day_of_week);
sdf.format(cal.getTime());

DateTimeFormatter dtf = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");
DateTime jodatime = dtf.parseDateTime(sdf.toString());

I would like to get a jodatime så that my calendar kan sort objects based on the date, the time inst necessary.

When I run the code and want to display the jodatime, I get this error:

java.lang.IllegalArgumentException: Invalid format: "java.text.SimpleDateFormat@b93b42a0"
    at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:945)
    at com.example.casper.autimeplan.Fragments.ScheduleFragment$MyJavaScriptInterface.getBasicInfo(ScheduleFragment.java:282)
    at com.example.casper.autimeplan.Fragments.ScheduleFragment$MyJavaScriptInterface.access$400(ScheduleFragment.java:186)
    at com.example.casper.autimeplan.Fragments.ScheduleFragment$MyJavaScriptInterface$1.run(ScheduleFragment.java:203)
Ali Azam
  • 2,047
  • 1
  • 16
  • 25
Bogses
  • 1

2 Answers2

1

tl;dr

LocalDate.now().with( WeekFields.ISO.weekOfWeekBasedYear(), weekOfYear )
               .with( WeekFields.ISO.dayOfWeek(), dayOfWeek )

java.time

Do not use the troublesome old date-time classes. Also, the Joda-Time project, now in maintenance mode, advises migration to the java.time classes. For Android, see the ThreeTenABP project mentioned in last bullets below.

You have not defined what you mean by week number. There are many ways to define a week of year. I will assume you meant the standard ISO 8601 definition of week # 1 having the first Thursday of the calendar, and Monday being the first day of every week.

Use the WeekFields class, specifically the WeekFields.ISO object.

long weekOfYear = 27 ; // 1-52 or 1-53 for ISO 8601 week-based years.
long dayOfWeek = 2 ;  // 1-7 for Monday-Sunday, per ISO 8601 standard.

LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) ) ;
LocalDate adjusted = today.with( WeekFields.ISO.weekOfWeekBasedYear(), weekOfYear )
                          .with( WeekFields.ISO.dayOfWeek(), dayOfWeek ) ;

Dump to console.

System.out.println( "2017-W27-02: " + adjusted ) ; 

2017-W27-02: 2017-07-04

See this code run live at IdeOne.com.

By the way… While not back-ported to older Android, other Java platforms can use the nifty YearWeek class in the ThreeTen-Extra library for this kind of work.


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?

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

You are simply passing toString of object which won't work. try something like this

private static String parseDateTime(String input){
     String pattern = "MM/dd/yyyy HH:mm:ss";
     DateTime dateTime  = DateTime.parse(input, DateTimeFormat.forPattern(pattern));
     return dateTime.toString("MM/dd/yyyy HH:mm:ss");
}

Read more here

vikas kumar
  • 10,447
  • 2
  • 46
  • 52