-1

I want a Quartz timer service for the update code below. Every six months, all users' keys should be updated according to their creationDate:

//dataTypes are date only

List<User> allDbUsers=userRepository.findAll();

Date currDate=new Date();
final long DAY_IN_MILLIS = 1000 * 60 * 60 * 24;
for(User user:allDbUsers)
{
    Date creationDate=user.getCreationDate();
    String firstKey=user.getFirstKey();

    Long diffInDays=(currDate.getTime()-creationDate.getTime())/DAY_IN_MILLIS;
    if(diffInDays==180)
    {
        user.setSecondKey(firstKey);
        user.setFirstKey("aH&#KK");
        userRepository.saveAndFlush(user);

    }
}   
Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
D.K
  • 1
  • 1
  • Are you sure you want to use a scheduler? Try to guess what happens if you schedule a job to run 180 days from now but your app gets restarted, say, once a month. – kryger Apr 23 '16 at 21:03
  • @kryger, his code not for run scheduler, it code for method job.excecute(what job must do). Scheduler should run every midnight(for example), then there will be problems if the server stops , after the launch night job will call again – HAYMbl4 Apr 24 '16 at 06:31

1 Answers1

0

You can use Calendar for view diff between dates, for example:

    Calendar currentDate = Calendar.getInstance();
    Calendar sixMonthAfterCreation;

    for (User user : allDbUsers) {
        sixMonthAfterCreation = Calendar.getInstance()
        sixMonthAfterCreation.setTime(user.getCreationDate());
        sixMonthAfterCreation.add(Calendar.MONTH, 6);

        String firstKey = user.getFirstKey();

        if (currentDate.after(sixMonthAfterCreation)) {
            user.setSecondKey(firstKey);
            user.setFirstKey("aH&#KK");
            userRepository.saveAndFlush(user);
        }
    }

or if you use java 8 you can convert you creation date to LocalDate and view diff.

If I right understand you, you want use Quartz Scheduler. I did not use it but I think you need implement org.quartz.Job, override execute method and write code for updating users in it.

For more information about Quartz Scheduler look this question

One clarification this code not for run scheduler, it code for method job.excecute(what job must do). Scheduler should run every midnight(for example), then every midnight will be called execute method which find all user for update and do it.

Community
  • 1
  • 1
HAYMbl4
  • 1,450
  • 2
  • 15
  • 29
  • hey Oleg Naumov thanks a ton for your time i am using java 8 but i need same code for exact (date,time with second) execution through Timer Quartz service how can we migrate this code for timer service i am using spring 4.2 annotations – D.K Apr 23 '16 at 14:18
  • @D.K. I updated my answer, sorry if it not helpful for you – HAYMbl4 Apr 23 '16 at 15:52
  • hi Oleg Naumov thank you for your update it is really helpful because in my previous code i was not getting exact time – D.K Apr 24 '16 at 05:31
  • Hii all it is not adding exact 6 months on whatever date i am getting from database can anyone help ?? I am getting month that is not correct means 1 month less it is comming – D.K May 18 '16 at 14:39