当前日期是前一周的日期

时间:2019-02-20 11:08:12

标签: java spring

我一直在检查有关获取当前日期的许多帖子,但是我不知道发生了什么。

有时,应用程序提供了错误的当前日期。因此,我添加了获取当前日期的第二种方法,因为报表必须显示在不同的位置,所以我俩都使用了它们。

它正在工作,但是今天我被告知不是。正在给13/02/2019,所以就在一周前。

代码是这样的:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd MMMM yyyy");
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
LocalDateTime today = LocalDateTime.now();
Date date = new Date();

addFormattedCell(table, paragraphFontNormal, dtf.format(today)  , Element.ALIGN_RIGHT, 1, 1);
addFormattedCell(table, paragraphFontNormal, sdf.format(date), Element.ALIGN_RIGHT, 1, 1);

两个人都给出了相同的错误日期(13/02/2019)。我再次部署了应用程序,并且可以正常工作。知道出什么问题了吗?

课程:

@Service
public class CropAcknowledgementServiceManager implements CropAcknowledgementService {

private static final Integer PAGE_SIZE = 10;

private Document document;

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd MMMM yyyy");
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
LocalDateTime today = LocalDateTime.now();
Date date = new Date();

private Font paragraphFontHead = FontFactory.getFont(FontFactory.HELVETICA, 10, Font.BOLD|Font.UNDERLINE);
private Font fontSmall = FontFactory.getFont(FontFactory.HELVETICA, 6, Font.NORMAL);
private Font fontSmallBold = FontFactory.getFont(FontFactory.HELVETICA, 6, Font.NORMAL|Font.BOLD);
private Font paragraphFontNormal =  new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL);
private Font paragraphFontSmall =  new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL);
private Font paragraphFontUnderline =  new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.UNDERLINE);

private List<SownSlrn> sownSlrn;


@Autowired
CropAcknowledgementRepository cropAcknowledgementRep;

@Autowired
SownSlrnRepository sownSlrnRep;

public void createPdf(CropAcknowledgement cropAcknowledgement, HttpServletResponse response) throws IOException, DocumentException {

    document = new Document();
    document.setMargins(50,50,70,80);
    PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
    document.open();

    addFotter(writer);

    PdfHeaderFooter event = new PdfHeaderFooter();
    writer.setPageEvent(event);

    List<CropAcknowledgement> report = findAll(cropAcknowledgement);

    addLetterHeading(report.get(0));
    addLetter(report.get(0), cropAcknowledgement.getName());
    addTableHeading(report.get(0));

    PdfPTable table = new PdfPTable(7);
    table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
    table.getDefaultCell().setFixedHeight(15);
    table.setWidthPercentage(920);
    table.setTotalWidth(new float[]{ 80, 20, 100, 140, 30, 100, 30 });
    table.setLockedWidth(true);

    table.addCell(new Paragraph("Variety", paragraphFontUnderline));
    table.addCell(new Paragraph("Cat", paragraphFontUnderline));
    table.addCell(new Paragraph("Crop ID", paragraphFontUnderline));
    table.addCell(new Paragraph("Grower's name", paragraphFontUnderline));
    addFormattedCell(table, paragraphFontUnderline, "Area", Element.ALIGN_RIGHT, 1, 1);
    table.addCell(new Paragraph("Sown SLRN", paragraphFontUnderline));
    table.addCell(new Paragraph("Insp.", paragraphFontUnderline));

    for(CropAcknowledgement ca : report){

        sownSlrn = sownSlrnRep.findByDescendent(ca.getSystemId());

        table.addCell(new Paragraph(ca.getVarietyName(), paragraphFontSmall));
        table.addCell(new Paragraph(ca.getCategoryEntered(), paragraphFontSmall));
        table.addCell(new Paragraph(ca.getCid(), paragraphFontSmall));
        table.addCell(new Paragraph(ca.getGrower(), paragraphFontSmall));
        if (ca.getArea() == 0.0) {
            addFormattedCell(table, paragraphFontSmall, "W/D", Element.ALIGN_RIGHT, 1, 1);
        }else{
            addFormattedCell(table, paragraphFontSmall, round(ca.getArea(), 1), Element.ALIGN_RIGHT, 1, 1);
        }
        boolean first =true;
        for (SownSlrn ss : sownSlrn){
            if (first) {
                table.addCell(new Paragraph(ss.getSownSlrn(), paragraphFontSmall));
                table.addCell(new Paragraph(ca.getInspFlag(), paragraphFontSmall));
                table.completeRow();
                first = false;
            }else{
                addFormattedCell(table, paragraphFontSmall, " ", Element.ALIGN_LEFT, 5, 1);
                addFormattedCell(table, paragraphFontSmall, ss.getSownSlrn(), Element.ALIGN_LEFT, 2, 1);
            }
        }
    }
    document.add(table);
    response.flushBuffer();
    document.close();
}

private void addLetterHeading(CropAcknowledgement ca) throws DocumentException {

    PdfPTable table = new PdfPTable(3);
    table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
    table.getDefaultCell().setFixedHeight(15);
    table.setWidthPercentage(500);
    table.setTotalWidth(new float[]{ 325, 75, 100 });
    table.setLockedWidth(true);

    addFormattedCell(table, paragraphFontNormal, ca.getPersonResponsible(), Element.ALIGN_LEFT, 3, 1);
    addFormattedCell(table, paragraphFontNormal, ca.getCropApplicant() , Element.ALIGN_LEFT, 3, 1);
    if (ca.getAddress1() != null) {
        addFormattedCell(table, paragraphFontNormal, ca.getAddress1() , Element.ALIGN_LEFT, 3, 1);
    }
    if (ca.getAddress2() != null) {
        addFormattedCell(table, paragraphFontNormal, ca.getAddress2() , Element.ALIGN_LEFT, 3, 1);
    }
    if (ca.getAddress3() != null) {
        addFormattedCell(table, paragraphFontNormal, ca.getAddress3() , Element.ALIGN_LEFT, 3, 1);
    }
    addFormattedCell(table, paragraphFontNormal, ca.getTown() , Element.ALIGN_LEFT, 3, 1);
    addFormattedCell(table, paragraphFontNormal, ca.getCounty() , Element.ALIGN_LEFT, 1, 1);
    addFormattedCell(table, paragraphFontNormal, "Applicant No:", Element.ALIGN_LEFT, 1, 1);
    addFormattedCell(table, paragraphFontNormal, ca.getRegNo().toString(), Element.ALIGN_RIGHT, 1, 1);
    addFormattedCell(table, paragraphFontNormal, ca.getPostCode(), Element.ALIGN_LEFT, 1, 1);
    addFormattedCell(table, paragraphFontNormal, "Date:", Element.ALIGN_LEFT, 1, 1);
    addFormattedCell(table, paragraphFontNormal, sdf.format(date), Element.ALIGN_RIGHT, 1, 1);

    document.add(table);
}

private void addLetter(CropAcknowledgement ca, String name) throws DocumentException {
    Paragraph line = new Paragraph(" ", paragraphFontNormal);
    document.add(line);
    document.add(line);
    document.add(line);
    line = new Paragraph("SEED CROP ENTRIES FOR HARVEST " + ca.getCidYr() , paragraphFontHead);
    line.setAlignment(Element.ALIGN_LEFT);
    document.add(line);
    document.add( Chunk.NEWLINE );
    document.add( Chunk.NEWLINE );
    line = new Paragraph(ca.getDearName() , paragraphFontNormal);
    document.add(line);
    document.add( Chunk.NEWLINE );
    line = new Paragraph("Enclosed is a list of the " + ca.getAdminGroupName()
            + " crop entries you have entered to date for harvest year " + ca.getCidYr() + ".", paragraphFontNormal);
    document.add(line);
    document.add( Chunk.NEWLINE );
    line = new Paragraph("Could you please check this list and notify us of any inaccuracies or omissions.  We have also indicated those crops which are subject to be officially inspected.", paragraphFontNormal);
    document.add(line);
    document.add( Chunk.NEWLINE );
    line = new Paragraph("Crops ....", paragraphFontNormal);
    document.add(line);
    line = new Paragraph("Crops ....", paragraphFontNormal);
    document.add(line);
    document.add( Chunk.NEWLINE );
    line = new Paragraph("" +
            " We would ....", paragraphFontNormal);
    document.add(line);
    document.add( Chunk.NEWLINE );
    document.add( Chunk.NEWLINE );
    line = new Paragraph("Yours sincerely,", paragraphFontNormal);
    document.add(line);
    document.add( Chunk.NEWLINE );
    line = new Paragraph(name, paragraphFontNormal);
    document.add(line);
    document.add( Chunk.NEWLINE );
    line = new Paragraph("", paragraphFontNormal);
    document.add(line);
    document.newPage();
}

private void addTableHeading(CropAcknowledgement ca) throws DocumentException {

    PdfPTable table = new PdfPTable(2);
    table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
    table.getDefaultCell().setFixedHeight(15);
    table.setWidthPercentage(500);
    table.setTotalWidth(new float[]{ 400, 100 });
    table.setLockedWidth(true);

    addFormattedCell(table, paragraphFontNormal, ca.getCropApplicant() + " - Applicant No " + ca.getRegNo(), Element.ALIGN_LEFT, 1, 1);
    addFormattedCell(table, paragraphFontNormal, dtf.format(today)  , Element.ALIGN_RIGHT, 1, 1);
    table.completeRow();
    document.add(table);

    Paragraph line = new Paragraph("Crop Entries for " + ca.getCidYr() + " - " + ca.getAdminGroupName() , paragraphFontUnderline);
    document.add(line);
    document.add( Chunk.NEWLINE );
    line = new Paragraph("Area = crop area entered minus any area withdrawn", paragraphFontNormal);
    document.add(line);
    line = new Paragraph("W/D = crop area completely withdrawn", paragraphFontNormal);
    document.add(line);
    line = new Paragraph("", paragraphFontNormal);
    document.add(line);
    document.add( Chunk.NEWLINE );
}

0 个答案:

没有答案