简体   繁体   English

Java Calendar.WEEK_OF_YEAR返回每月的一周

[英]Java Calendar.WEEK_OF_YEAR returns week of the month

/* package whatever; // don't place package name! */
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone {

    public static void main (String[] args) throws java.lang.Exception {
        try {
            // your code goes here
            SimpleDateFormat sdf= new SimpleDateFormat("dd-mm-yyyy");
            Calendar cal= Calendar.getInstance();
            String s = "12-12-2014";
            Date dte=sdf.parse(s);
            cal.setTime(dte);

            System.out.println( cal.get(Calendar.WEEK_OF_YEAR)+"");
        } catch (Exception e ) {}
    }
}

Output is 3 . 输出为 3

I am trying to get week of the year from a given date. 我正在尝试从给定日期获得一年中的一周。 I referred java docs but the WEEK_OF_YEAR should be in 50 . 我引用了Java文档,但WEEK_OF_YEAR应该在50

Your SimpleDateFormat is wrong, see JavaDoc : 您的SimpleDateFormat错误,请参阅JavaDoc

Letter | 信| Date or Time | 日期或时间 Component | 组件| Presentation | 简报 Examples 例子

m | 米| Minute in hour | 一小时内| Number | 编号 30 30

It should be: 它应该是:

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM