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 : https://docs.camunda.org/manual/7.12/reference/rest/task/get/
for history you can check this : https://docs.camunda.org/manual/7.12/reference/rest/history/task/get-task-query/ 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:
taskService.createTaskQuery().taskId(your_task_id).list()