How to write and deploy new workflow version changing existing workflow instance?

I have written a pseudo code for 2 version. I need to deploy version 2 with below constraints

  1. Skip order creation step
  2. Wait for notification if it has more than 24 hours to cancel
  3. Send the notification if it is less than or equal to 24 hours window and wait for cancellation

Can some one help in explaining how to deploy the new version and how to modify all the existing workflow instances to new version?

Version 1

  1. Create order
  2. Wait for auto cancellation duration if the order is pending
/* Version 1: Auto cancellation */
public void execute(byte [] orderRequest) {
    order = create(Workflow.getInfo().getWorkflowId(), orderRequest);
    Workflow.await(getAutoCancellationDuration(), () -> !this.order.getStatus().equals("PENDING"));
    switch (this.order.getStatus()) {
        case "PENDING":
            this.order.setStatus("CANCELLED");
            logger.info("order {} auto cancellation triggered", order.getId());
            //triggerAutoCancellationWorkflow
            break;
        case "CANCELLED":
            logger.info("order {} manual cancellation triggered", order.getId());
            //triggerManualCancellationWorkflow
            break;
        case "APPROVED":
            logger.info("order {} approval triggered", order.getId());
            //triggerApprovalWorkflow
            break;
    }
    Workflow.await(getArchiveDuration(), () -> this.order.getStatus().equals("ARCHIVE"));
}

Version 2

  1. Create order
  2. Trigger an email before 24hrs of cancellation
  3. Wait for auto cancellation duration if the order is pending
/* Version 2: Prior notification prior before auto cancellation */
public void execute(byte [] orderRequest) {
    order = create(Workflow.getInfo().getWorkflowId(), orderRequest);
    Workflow.await(getAutoCancellationDuration()-24hrs, () -> !this.order.getStatus().equals("PENDING"));
    if(this.order.getStatus().equals("PENDING")) {
        sendNotification();
    }
    Workflow.await(currentTime+24hrs, () -> !this.order.getStatus().equals("PENDING"));
    switch (this.order.getStatus()) {
        case "PENDING":
            this.order.setStatus("CANCELLED");
            logger.info("order {} auto cancellation triggered", order.getId());
            //triggerAutoCancellationWorkflow
            break;
        case "CANCELLED":
            logger.info("order {} manual cancellation triggered", order.getId());
            //triggerManualCancellationWorkflow
            break;
        case "APPROVED":
            logger.info("order {} approval triggered", order.getId());
            //triggerApprovalWorkflow
            break;
    }
    Workflow.await(getArchiveDuration(), () -> this.order.getStatus().equals("ARCHIVE"));
}

Versioning docs (Java SDK) and versioning intro video.
Example (part of Java workshop), and associated video.

how to deploy the new version and how to modify all the existing workflow instances to new version

I think the above links and samples should get you started in the right direction to version the parts of code that have changed between your two examples, namely the code between when you create a new order and the switch statement. To apply the changes you would need to restart your worker processes.
For existing executions, watch the intro video it has visual examples of how versioning works in relations to where in the execution process the currently running executions are.

If you get stuck feel free to share your code again and we can take deeper look.

On a side note, does your create method run activities? If so it seems you might not be handling ActivityFailure. Also if it does call activities, you can get the workflow id inside activity code via:

Activity.getExecutionContext().getInfo().getWorkflowId();

and don’t have to pass it in as param.