how to add 2 months to Timestamp?

Posted on 2007-08-08
Last Modified: 2013-11-05
add 2 months to Timestamp?
Question by:chaitu chaitu
    LVL 92

    Accepted Solution

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MONTH, 2);
    ts = new Timestamp(cal.getTime().getTime());
    LVL 2

    Expert Comment

    GregorianCalendar lGregorianCalendar = new GregorianCalendar();
    lGregorianCalendar.setTimeInMillis( ts.getTime() );
    lGregorianCalendar.add(Calendar.MONTH, 2);
    ts.setTime( lGregorianCalendar.getTimeInMillis() );
    LVL 2

    Expert Comment

    hmm, objects answered while I had the question open
    LVL 2

    Expert Comment

    Ehm, objects... Your solution IMHO has a flaw: it does not copy the nano seconds part of Timestamp. (Timestamp is milleseconds + nanoseconds), you create a new TS without copy the nano seconds part.

    Because my approach reuses the original Timestamp, it does not have that problem.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    wordsWithout 49 64
    Issues installing SSL certificate into Apache Tomcat 3 48
    firstChar challenge 13 57
    @SBGen Method 3 16
    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now