Get process instance from task

For certain actions where a user wants to proceed in the workflow with a UserTask, I have the taskId and use taskService#complete in order to complete it.

At that point I want to get the processInstance because I have to make some modifications on a business object which has the process instance id stored.

How would I be able to get the processInstanceId when I have the taskId? I understand that a Task is also an Activity and have seen in another question that I could somehow use a processInstanceQuery with the activityId (I guess the taskId is also an activityId). And if yes, how can I use a processInstanceQuery to get the processInstanceId.

you have the taskId so you can simply hit this api route :
for runing task :
for history you can check this : by sending taskId as query parameters
and you will find processInstanceId in response

Is there also a way I can obtain the processInstanceId via the Java API?

Try this one: