使用Java中的Criteria Builder API进行日期比较

时间:2017-09-15 10:39:13

标签: java date datetime jpa-2.0 criteria-api

我想使用条件API将登录用户的当前日期(根据他的时区)与表格列进行比较。

但是我无法从我为用户计算的本地日期中得到一个表达式。我该如何解决这个问题?

public void checkTaskCategory(TaskCriteria criteria, CriteriaBuilder cb, CriteriaQuery<Task> q, Root<Task> task, List<Predicate> predicates) {
    if (StringUtils.isNotBlank(criteria.getCategory())) {
     LocalDate currentDateForUserTimezone = LocalDateTime.now(ZoneId.of("Pacific/Pago_Pago")).toLocalDate();
        if (StringUtils.equalsIgnoreCase(TaskConstants.TASK_OVERDUE, criteria.getCategory())) {
            predicates.add(cb.greaterThan(cb.currentDate(), task.<Date>get(TaskConstants.DUE_DATE)));
            predicates.add(cb.notEqual(task.get("status"), getStatus(Status.Value.DONE.toString()).getId()));
            q.orderBy(cb.asc(task.get(TaskConstants.DUE_DATE)));
        }
    }
}

我想将cb.currentDate()替换为我的计算currentDateForUserTimezone进行比较,因为cb.currentDate没有考虑时区。我怎样才能实现它?

1 个答案:

答案 0 :(得分:0)

最后,我得到了解决方案。我们需要使用ParameterExpression来比较数据库中存在的列值和新的用户定义对象值。然后在setParameter中使用相同的别名来使用该值。代码将改变如下:

\documentclass{book}
\addtolength{\topmargin}{-.5in}
\addtolength{\headsep}{.5in}
\oddsidemargin=.5in
\evensidemargin=.5in
\textwidth=5.5in
\usepackage{
amsmath,% AMS basic math stuff
amsthm,% AMS theorem defining stuff
amsfonts,% defines the blackboard bold fonts for \Z, \R, etc
longtable,% used to create the tcproof environment below
verbatim,% allows for verbatim output, and also covering up stuff in comments
xspace,% adds an extra space an the end of some commands
multicol,% allows multicolumn output
tikz,% creates the tikzpicture drawing environment
charter,% changes the default font to charter
framed,% used to color in the TIscreen environment below
bm,
mathrsfs}
\usepackage{pifont}
\usepackage[colorlinks,unicode]{hyperref}

\newcommand{\R}{\mathbb{R}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Tau}{\bm{\mathcal{T}}}
\newcommand{\Taup}{\bm{\mathcal{T}^{\prime}}}
\newcommand{\Bp}{\mathscr{B}^{\prime}}
\newcommand{\B}{\mathscr{B}}
\newcommand{\CCp}{\mathscr{C}^{\prime}}
\newcommand{\CC}{\mathscr{C}}
\newcommand{\Ltop}{\R_{\mathscr{L}}}
\newcommand{\Ktop}{\R_{\mathscr{K}}}

\DeclareMathOperator{\lcm}{lcm}

\newcommand{\cl}[1]{[#1]}

\newcommand{\st}{\mid}

\newcommand{\eq}{\stackrel{?}{=}}

\newcommand{\divq}{\stackrel{?}{|}}

\newcommand{\forwards}{\mbox{``$\Longrightarrow$''}\xspace}
\newcommand{\backwards}{\mbox{``$\Longleftarrow$''}\xspace}


\newcommand{\define}[1]{\textbf{#1}}

\newcommand{\threeven}{pretty\xspace}
\newcommand{\throd}{normal\xspace}
\newcommand{\throve}{ugly\xspace}


\DeclareMathSymbol{\nmid}{\mathrel}{AMSb}{"2D}
\newcommand{\notdiv}{\nmid}


\renewcommand{\tilde}{\widetilde}


\renewcommand{\thefootnote}{\fnsymbol{footnote}}

\def\endclass#1{\par\noindent\hrulefill\fbox{\tiny This is where we
    ended on #1}\hrulefill\vskip 5pt plus 1pt\par }


\pagestyle{headings}
\makeatletter

\edef\today{%
  \the\year/\two@digits{\the\month}/\two@digits{\the\day}}

\renewcommand{\@evenhead}{\emph{Ordinary Differential Equations - Arnold}
   (v. \today)
    \hfill Krishna Bhogaonker \hfill \thepage}
\renewcommand{\@oddhead}{(version \today) \hfill \thepage}
\makeatother

\DeclareMathSymbol{\varnothing}{\mathord}{AMSb}{"3F}
\renewcommand{\emptyset}{\varnothing}

\colorlet{shadecolor}{gray!35}
\newenvironment{TIscreen}
{\begin{center}\tt
\renewcommand{\in}[1]{##1\\}
\newcommand{\out}[1]{\mbox{}\hfill##1\\}
\begin{minipage}{2in}\begin{snugshade}}
{\end{snugshade}\end{minipage}\end{center}}



\newenvironment{tcproof}[1]
{\smallskip\par\begin{longtable}{@{}p{.45\textwidth}p{.45\textwidth}@{}}
\multicolumn{2}{@{}l}{\emph{#1}}\\[\smallskipamount]
Assertion & Justification \endfirsthead
Assertion & Justification \endhead
\hline }
{\end{longtable}\qed\smallskip\par}

\newtheorem{lemma}{Lemma}[section]
\newtheorem{theorem}[lemma]{Theorem}
\newtheorem{cor}[lemma]{Corollary}
\newtheorem*{scholium}{Scholium}


\theoremstyle{definition}
\newtheorem{definition}[lemma]{Definition}
\newtheorem{example}[lemma]{Example}
\newenvironment{comments}{}{}

\makeatletter

  \define@key{hide}{scholium}[true]{\renewenvironment{scholium}{\comment}{\endcomment}}
  \define@key{hide}{proof}[true]{\renewenvironment{proof}{\comment}{\endcomment}}
  \define@key{hide}{lemma}[true]{\renewenvironment{lemma}{\comment}{\endcomment}}
  \define@key{hide}{comments}[true]{\renewenvironment{comments}{\comment}{\endcomment}}
  \define@key{hide}{cor}[true]{\renewenvironment{cor}{\comment}{\endcomment}}
  \define@key{hide}{definition}[true]{\renewenvironment{definition}{\comment}{\endcomment}}
  \define@key{hide}{example}[true]{\renewenvironment{example}{\comment}{\endcomment}}
  \define@key{hide}{theorem}[true]{\renewenvironment{theorem}{\comment}{\endcomment}}

\newcommand{\HideEnvirons}[1]{\setkeys{hide}{#1}}


  \define@key{show}{scholium}[true]{\define@key{hide}{scholium}{}}
  \define@key{show}{proof}[true]{\define@key{hide}{proof}{}}
  \define@key{show}{lemma}[true]{\define@key{hide}{lemma}{}}
  \define@key{show}{comments}[true]{\define@key{hide}{comments}{}}
  \define@key{show}{cor}[true]{\define@key{hide}{cor}{}}
  \define@key{show}{definition}[true]{\define@key{hide}{definition}{}}
  \define@key{show}{example}[true]{\define@key{hide}{example}{}}
  \define@key{show}{theorem}[true]{\define@key{hide}{theorem}{}}

\newcommand{\ShowEnvirons}[1]
{\setkeys{show}{#1}\HideEnvirons{%
    comments,
    cor,
    definition,
    example,
    proof,
    theorem,
    lemma,
    scholium
  }}
\makeatother



\begin{document}
\tableofcontents
\newpage


\chapter{Basic Concepts}

${0}




\end{document}

然后在setParameter中使用别名“currentDate”并在其中设置值currentDateForUserTimezone,它将与所需的字段值进行比较。 }