How to generate a computed property by method using gson?


This Content is from Stack Overflow. Question asked by Acech

I have a abstract class below and annotated @SerializedName on methods,
and expected that Gson should generate “findOffsetDuration” in final result, but it didn’t.
Anyway to generate that computed property?

    public abstract static class BaseSyncTimedReport extends TimedReport {
        protected Date findOffsetEndTime = new Date();
        protected Date findNextBatchEndTime = new Date();
        protected Date batchSyncEndTime = new Date();

        public long findOffsetDuration() {
            return Duration.between(startTime.toInstant(), findOffsetEndTime.toInstant()).toMillis();

        public long findNextBatchDuration() {
            return Duration.between(findOffsetEndTime.toInstant(), findNextBatchEndTime.toInstant()).toMillis();

        public long batchSyncDuration() {
            return Duration.between(findNextBatchEndTime.toInstant(), batchSyncEndTime.toInstant()).toMillis();


check the solution

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?