Question

Good day all,

I was trying to create a date variable by using Calendar java, the following is my example code:

long day = System.currentTimeMillis();
Calendar cal = Calendar.getInstance();
java.text.SimpleDateFormat month = new java.text.SimpleDateFormat("MMM"); // not sure how to assign this value inside
cal.setTimeInMillis(day);
cal.set(Calendar.HOUR_OF_DAY, 00);
cal.set(Calendar.MINUTE, 00);
cal.set(Calendar.SECOND, 00);
cal.set(Calendar.MILLISECOND, 000);
Date todayDate = cal.getTime();

Timestamp current = new Timestamp(todayDate.getTime());

However, the value of current I get is 2014-01-13 00:00:00.0. I prefer to set the 01 to Jan instead.

Any ideas on this?

Was it helpful?

Solution

You mean something like so?

        long day = System.currentTimeMillis();
        Calendar cal = Calendar.getInstance();
        java.text.SimpleDateFormat month = new java.text.SimpleDateFormat("yyyy-MMM-dd HH:mm:ss:S"); // not sure how to assign this value inside
        cal.setTimeInMillis(day);
        cal.set(Calendar.HOUR_OF_DAY, 00);
        cal.set(Calendar.MINUTE, 00);
        cal.set(Calendar.SECOND, 00);
        cal.set(Calendar.MILLISECOND, 000);
        Date todayDate = cal.getTime();

        Timestamp current = new Timestamp(todayDate.getTime());
        System.out.println(month.format(current));

Yields:

2014-Jan-13 00:00:00:0

Please check the SimpleDateFormat for more formatting options.

OTHER TIPS

You can try this

    long day = System.currentTimeMillis();
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat month = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss:S");
    cal.setTimeInMillis(day);     
    cal.set(Calendar.HOUR_OF_DAY, 00);
    cal.set(Calendar.MINUTE, 00);
    cal.set(Calendar.SECOND, 00);
    cal.set(Calendar.MILLISECOND, 000);
    Date todayDate = cal.getTime();

    Timestamp current = new Timestamp(todayDate.getTime());
    System.out.println(month.format(current));

Now out put:

    2014-Jan-13 00:00:00:0

ideone

// not sure how to assign this value inside

there you go.. ;)

yy = year (2 digit)
yyyy = year (4 digit)
M = month of the year
MM = month of the year (leading 0)
MMM = month of the year (short text)
MMMM = month of the year (full text)
d = day of the month
dd = day of the month (leading 0)
h = hour (1-12)
H = hour (0-23)
m = minute of the hour
s = second of the minute
S = milisecond
E = day of the week (short)
EEEE = day of the week (full)
D = day of the Year

Additional link to the SimpleDateFormat class

Since you are looking for current date, you do not need currentTimeMillis().

Instead you can do the following to get the desired output.

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss.S");
System.out.println(sdf.format(cal.getTime()));

tl;dr

Avoid the troublesome legacy date-time classes. Use only java.time classes.

LocalDate.now()                  // Determine the current date as seen in the wall-clock time in use by JVM’s current default time zone. Better to specify explicitly your desired/expected time zone.
         .getMonth()             // Get the `Month` enum object representing the month of that `LocalDate` object’s date value.
         .getDisplayName(        // Let the `Month` enum object automatically localize to generate the string of the name of the month.
             TextStyle.SHORT ,   // Specify how long or abbreviate you want the text of the name of the month.
             Locale.US           // Specify a `Locale` to determine the human language and cultural norms to be used in localizing the name of the month.
         )                       // Return a String.

Jan

java.time

The modern approach uses java.time classes.

Getting the current month means determining the current date. And getting the current date requires a time zone, as for any given moment the date varies around the globe by zone.

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate ld = LocalDate.now( z ) ;
Month m = ld.getMonth();

Let the Month enum automatically localize the name of the month. Control abbreviation by passing a TextStyle.

String output = m.getDisplayName( TextStyle.SHORT , Locale.US );

Apr

Date with time

If your goal is to combine a time-of-day with your date, use ZonedDateTime class. You must specify a time zone to determine the first moment of the day. The day does not start at 00:00:00 on some dates in some zones.

ZonedDateTime zdt = ld.atStartOfDay( z ) ;  // Determine the first moment of the day.

Do not use java.sql.Timestamp for the purpose of generating a String. Use the DateTimeFormatter class, documented in many other Answers on Stack Overflow.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MMM-dd HH:mm:ss" , Locale.US ) ;
String output = zdt.format( f ) ;

Or just call ZonedDateTime::toString to generate a String in standard ISO 8601 format, wisely extended to append the name of the zone in square brackets.

String output = zdt.toString() ;

Indeed, do not use Timestamp at all. That class is intended for use with databases, and is now replaced by Instant as of JDBC 4.2 and later.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top